I would like to enable editing a JTable cell on a key, say F2.
I know that by default double clicking will enable editing, but is there a way to bind that event to a key? I tried this link JTable edit on keypress but it doesn't work for me.
Here is my code:
public class DatabaseJTable extends JTable implements MouseListener {
public DatabaseJTable(Object [][] data, Object [] columnNames) {
super(data, columnNames);
InputMap inputMap = this.getInputMap(JComponent.WHEN_FOCUSED);
ActionMap actionMap = this.getActionMap();
this.addMouseListener(this);
// bind edit record to F2 key
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0), "edit");
actionMap.put("edit", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent ae) {
// TODO Auto-generated method stub
DatabaseJTable table = (DatabaseJTable)ae.getSource();
table.changeSelection(table.getSelectedRow(), 1, false, false);
table.editCellAt(table.getSelectedRow(), 1);
System.out.println("F2 pressed");
}
});
// binding delete record to Delete key
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "delete");
actionMap.put("delete", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent ae) {
// TODO Auto-generated method stub
}
});
}
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
}
Thanks in advance.