0

The default behaviour of JTable is , by pressing Tab or Enter to move to the next editable cell . But what I want is like this , when I press Enter I need to edit the cell insteading of moving to the next cell. How to implement this , thanks in advance.

xuqin1019
  • 202
  • 3
  • 12

1 Answers1

1

Swing was designed to use Key Bindings (see the Swing tutorial on How to Use Key Bindings). That is you bind an Action to a KeyStroke.

By default:

  1. The Enter key will move the cell selection to the next row
  2. The F2 key will place a cell in edit mode

You want to replace the default Action of the Enter key with the Action of the F2 key. This is easily done by using Key Bindings:

InputMap im = table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
KeyStroke f2 = KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0);
im.put(enter, im.get(f2));

Also, check out Key Bindings for a list of the default bindings for all Swing components.

camickr
  • 321,443
  • 19
  • 166
  • 288