tags:

views:

309

answers:

2

This is possibly has a trivial solution, but I am at the end of my tether so I hope somebody can help out.

I use a JTable which has a custom renderer and a custom editor for a set of columns.
The renderer uses a JLabel component and the editor uses a JSpinner component.
Our users want to be able to enter values in a column, and then press TAB or ENTER to move to the next editable cell in the table.
If I understand correctly, this is the default behaviour for a JTable.

However, this doesn't seem to work correctly for me. Until the user clicks on the cell, only the JLabel is displayed.
The JSpinner (i.e. CellEditor) is only displayed when a user double clicks on the cell. So, it looks like the cell is going into "edit" mode only on MouseEvents, but not when it has focus.

How do I get the cell to go into edit mode as soon as it has focus?

+1  A: 

You can achieve this programatically, you simply listen to the focus events on the cell, on focus and editing allowed, start editing.

More on this thread and example

n002213f
+3  A: 

Thank you n00213f. The thread and example from your post were helpful. By overloading the changeSelection method in JTable as hinted to in the thread, JTable checks if a cell is editable every time the selection is changed. If the sell is editable, it will show the CellEditor and transfer focus to the editor component.

For completeness, here is my solution:

  JTable myTable = new javax.swing.JTable()
  {
            public void changeSelection(final int row, final int column, boolean toggle, boolean extend)
            {
                super.changeSelection(row, column, toggle, extend);
                myTable.editCellAt(row, column);
                myTable.transferFocus();
            }
  };
Luhar
good to hear the answer was useful.
n002213f