I needed to delete rows from a JTable on the delete key-press. So the use case is quite simple, the user select some rows, press the delete key, the rows get deleted. The co
Use key bindings instead...
InputMap im = table.getInputMap(JTable.WHEN_FOCUSED);
ActionMap am = table.getActionMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "DeleteRow");
am.put("DeleteRow", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Delete row");
int row = table.getSelectedRow();
if (row > -1) {
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.removeRow(row);
}
}
});
(I borrowed mKorbel data for my test, so my test was using a DefaultTableModel
, you will need to cast to the model you are using).
Also, if you editing, this may still fire, so you will need to check for that