避免KeyListener
,当涉及到焦点和事件的调度顺序时,它是不可靠的(可能在您的侦听器之前使用它们的键,因此您永远不会收到通知)。
改用键绑定
InputMap im = table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
ActionMap am = table.getActionMap();
KeyStroke enterKey = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
im.put(enterKey, "Action.enter");
am.put("Action.enter", new AbstractAction() {
public void actionPerformed(ActionEvent evt) {
table.changeSelection(table.getSelectedRow(), 2, false, false);
if (!table.editCellAt(table.getSelectedRow(), 2)) {
JOptionPane.showMessageDialog(table, "Failed to start cell editing");
}
}
});
我也怀疑这个myTab.changeSelection(myTab.getSelectedRow(), 2, true, false);
电话。JavaDocs 基本上说...
切换:真,扩展:假。如果选择了指定的单元格,请取消选择它。如果未选择,请选择它。
这向我建议,如果当前选择了单元格,它将被取消选中。
更新了工作示例
public class TestTableEditor {
public static void main(String[] args) {
new TestTableEditor();
}
private JTable table;
public TestTableEditor() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
table = new JTable(new MyTableModel());
InputMap im = table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
ActionMap am = table.getActionMap();
KeyStroke enterKey = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
im.put(enterKey, "Action.enter");
am.put("Action.enter", new AbstractAction() {
public void actionPerformed(ActionEvent evt) {
table.changeSelection(table.getSelectedRow(), 1, false, false);
if (!table.editCellAt(table.getSelectedRow(), 1)) {
JOptionPane.showMessageDialog(table, "Failed to start cell editing");
}
}
});
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class MyTableModel extends AbstractTableModel {
@Override
public int getRowCount() {
return 1;
}
@Override
public int getColumnCount() {
return 3;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Object value = null;
switch (columnIndex) {
case 0:
value = "Can't edit";
break;
case 1:
value = "Edit me";
break;
case 2:
value = "Can't edit";
break;
}
return value;
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex == 1;
}
}
}
更新
在所有愚蠢,痛苦,难以找到的...
添加table.setSurrendersFocusOnKeystroke(true);
到您的代码...
设置当由于 JTable 转发单元格的键盘事件而激活编辑器时,此 JTable 中的编辑器是否获得键盘焦点。默认情况下,此属性为 false,除非单击单元格,否则 JTable 将保留焦点。