我在JTable
中遇到光标问题。
我试图在论坛中找到答案,但找不到我期望的答案。
这是我的可运行Java:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.table.AbstractTableModel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.JTable;
public class Fpos extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Fpos frame = new Fpos();
frame.setVisible(true);
frame.setLocationRelativeTo(null); //make frame center of screen
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Fpos() {
//create Jpanel
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(10, 10, 1300, 700);
JPanel contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
//create label TOTAL
JLabel lblTotal = new JLabel("TOTAL : Rp.");
lblTotal.setFont(new Font("Wide Latin", Font.PLAIN, 30));
lblTotal.setBounds(33, 25, 312, 31);
contentPane.add(lblTotal);
//create label Total Amount
JLabel lblNewLabel = new JLabel("123,456,789");
lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
lblNewLabel.setFont(new Font("Wide Latin", Font.PLAIN, 60));
lblNewLabel.setBounds(571, 6, 659, 61);
contentPane.add(lblNewLabel);
//create jtable in scrollpane
JTable table = new JTable(new MyTableModel());
JScrollPane sp=new JScrollPane(table);
sp.setBounds(20,76,1240,572);
contentPane.add(sp);
}
//tablemodel
class MyTableModel extends AbstractTableModel {
private String[] columnNames = {"PLU", "NAME", "UOM", "QTY", "PRICE","AMOUNT"};
private Object[][] data = {{"", "", "", new Double(0), new Integer(0), new Integer(0)}};
public int getColumnCount() {return columnNames.length;}
public int getRowCount() {return data.length;}
// public String getColumnName(int col) {return columnNames[col];}
public Object getValueAt(int row, int col) {return data[row][col];}
//auto formating table: string=left alignment, numeric=right alignment, checkbox=check box not true/false
public Class getColumnClass(int c) {return getValueAt(0, c).getClass();}
//make table editable only for just first column
public boolean isCellEditable(int row, int col) {if (col == 0) {return true;} else{return false;}}
//make table can change value
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
}
}
}
输出很好但是表格还没准备好输入。 我必须双击PLU第一行的列以准备输入。 我想要的是,一旦我运行它,光标在PLU第一行闪烁,准备输入而不是双击它。
有关如何实现这一目标的任何建议吗?
答案 0 :(得分:0)
基础知识将是:
table.changeSelection(0, 0, false, false);
if (table.editCellAt(0, 0))
{
Component editor = table.getEditorComponent();
editor.requestFocusInWindow();
//((JTextComponent)editor).selectAll();
}
changeSelection(...)
就像点击该行(因此整个行突出显示),然后editCellAt(...)
将单元格置于编辑模式。
然后您需要将焦点放在编辑器上,并可选择选择所有文本,以便在您键入时将其替换。
编辑:
光标仍然没有闪烁
将代码包裹在SwingUtilities.invokeLater(...)
中以确保代码在框架可见后执行:
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
table.changeSelection(0, 1, false, false);
if (table.editCellAt(0, 1))
{
Component editor = table.getEditorComponent();
editor.requestFocusInWindow();
//((JTextComponent)editor).selectAll();
}
}
});
答案 1 :(得分:0)
是的,添加的代码会使光标在选定的单元格n上闪烁,以便为输入做好准备。
非常感谢Camickr。
我将下面的可运行代码放在下面,万一有人碰巧遇到同样的情况。
import java.awt.Component;
import java.awt.EventQueue;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.JScrollPane;
导入javax.swing.border.EmptyBorder;
import javax.swing.table.AbstractTableModel;
导入javax.swing.JLabel;
import java.awt.Font;
导入javax.swing.SwingConstants;
导入javax.swing.SwingUtilities;
导入javax.swing.JTable;
public class test扩展了JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
test frame = new test();
frame.setVisible(true);
frame.setLocationRelativeTo(null); //make frame center of screen
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public test() {
//create Jpanel
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(10, 10, 1300, 700);
JPanel contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
//create label TOTAL
JLabel lblTotal = new JLabel("TOTAL : Rp.");
lblTotal.setFont(new Font("Wide Latin", Font.PLAIN, 30));
lblTotal.setBounds(33, 25, 312, 31);
contentPane.add(lblTotal);
//create label Total Amount
JLabel lblNewLabel = new JLabel("123,456,789");
lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
lblNewLabel.setFont(new Font("Wide Latin", Font.PLAIN, 60));
lblNewLabel.setBounds(571, 6, 659, 61);
contentPane.add(lblNewLabel);
//create jtable in scrollpane
JTable table = new JTable(new MyTableModel());
JScrollPane sp=new JScrollPane(table);
sp.setBounds(20,76,1240,572);
contentPane.add(sp);
//make cursor blinking on selected cell
SwingUtilities.invokeLater(new Runnable(){
public void run() {
table.changeSelection(0, 0, false, false);
if (table.editCellAt(0, 0)){
Component editor = table.getEditorComponent();
editor.requestFocusInWindow();
//((JTextComponent)editor).selectAll();
}
}
});
}
//tablemodel
class MyTableModel extends AbstractTableModel {
private String[] columnNames = {"PLU", "NAME", "UOM", "QTY", "PRICE","AMOUNT"};
private Object[][] data = {{"", "", "", new Double(0), new Integer(0), new Integer(0)}};
public int getColumnCount() {return columnNames.length;}
public int getRowCount() {return data.length;}
// public String getColumnName(int col){return columnNames [col];}
public Object getValueAt(int row,int col){return data [row] [col];}
//自动格式化表:string =左对齐,数字=右对齐,复选框=复选框不是真/假
public class getColumnClass(int c){return getValueAt(0,c).getClass();}
//使表格仅适用于第一列
public boolean isCellEditable(int row,int col){if(col == 0){return true;} else {return false;}}
// make table可以改变值
public void setValueAt(Object value,int row,int col){
data [row] [col] = value;
fireTableCellUpdated(row,col);
}
}
}