I have a keylistener on jtable so that when someone presses enter some calculations happen. However, this only happens if the person is not editing. I would like to apply this action when a person finishes editing a cell and is pressing enter to finish and close the editing.
I cannot figure this out, anyone ever did this or know how to?
Basically, now for the action to be done, people must press enter twice, one to end the editing and another for the action that I want to happen, I would like to make it needed only once, while editing.
Thank you
You could customize your own editor. Using DefaultCellEditor Instead using KeyListener you should use KeyBindings.
See this example.
JTable table = new JTable(myModel);
JTextField cell = new JTextField();
final TableCellEditor cellEditor = new DefaultCellEditor(cell);
table.getColumnModel().getColumn(column).setCellEditor(cellEditor);
InputMap iMap = cell.getInputMap(JComponent.WHEN_FOCUSED);
iMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), KeyEvent.getKeyText(KeyEvent.VK_ENTER));
ActionMap aMap = cell.getActionMap();
aMap.put(KeyEvent.getKeyText(KeyEvent.VK_ENTER), new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
if(callSomeOperationsIsOk()){
cellEditor.stopCellEditing();
}else{
cellEditor.cancelCellEditing();
}
}
});
}
Read more in tutorials How to use Tables, and perhaps you have the same trouble that i have see my previous question