试试这个:
table.setDefaultEditor(Object.class,new TableEditor());
class TableEditor extends DefaultCellEditor{
public TableEditor(){
super(new JTextField());
setClickCountToStart(1);
getComponent().addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_ENTER)
System.out.println("enter");
}
});
}
public Component getTableCellEditorComponent(JTable table,Object value,boolean isSelected,int row,int column){
JTextField com=(JTextField)super.getTableCellEditorComponent(table,value,isSelected,row,WIDTH);
com.setText((String)value);
return com;
}
}
和
table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0),"");
如果需要,删除 Enter 键的标准反应
或者:
editorComponent.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0),"ent");
editorComponent.getActionMap().put("ent",new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e){
System.out.println("opp");
}
});
在 TableEditor 构造函数中(而不是“getComponent”人员)。