1

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.

Community
  • 1
  • 1
lightbringer
  • 835
  • 2
  • 12
  • 24

1 Answers1

4

F2 already is the default KeyStroke used by JTable to start editing.

See Key Bindings for a table of all the KeyStrokes used by all the components. You will also find examples of using key bindings.

If you do create you own Action, instead of using the provide Action then the code should be something like:

int row = table.getSelectedRow();
int column = table.getSelectedColumn();

if (editCellAt(row, column))
{
    Component editor = table.getEditorComponent();
    editor.requestFocusInWindow();
}

So the editor gets focus once the key is pressed.

Apparently, Aqua LAF doesn't bind F2 so it looks like you need to do it yourself. Assuming the "startEditing" Action is defined in the ActionMap you can use:

KeyStroke keyStroke = KeyStroke.getKeyStroke("F2");
InputMap im = table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
im.put(keystroke, "startEditing");
camickr
  • 321,443
  • 19
  • 166
  • 288
  • For reference, `com.apple.laf.AquaLookAndFeel` does not bind `F2` (or any key) to `startEditing`. – trashgod Nov 08 '13 at 04:32
  • @trashgod, Interesting, is the startEditing Action defined? – camickr Nov 08 '13 at 04:33
  • thanks for the information. I have a minor question. Double clicking on a cell will trigger editing as well. Is there a way to disable that so only F2 can trigger editing? – lightbringer Nov 08 '13 at 06:36
  • @ightbringer its not good idea to consume() this mouse events or override that somehow, you can to change DefaultCellEditor#setClickCountToStart(int) to reasonable number 4-5, let it this accelerator accessible from mouse events too – mKorbel Nov 08 '13 at 06:59
  • the reason is I want to double clicking to a row to open a new Panel, and F2 to edit that row. Thanks for the suggestion, I'll try it. – lightbringer Nov 08 '13 at 08:26