0

Here is what I want. I want tab to go to the next editable cell and shift+tab to go to the previous editable field. I think I may be going down a terrible path right now from some code I found out there.

Right now, I'm extending JTable and overriding the changeSelection method. I'm looking at the current event and saying "if this is a tab event, go to the next editable field". This works, except in the case where the user is currently editing and then they hit tab. In that case, a HierarchyEvent happens with (as far as I can tell) no indication that the hierarchy change came from a tab or a mouse click, or whatever. So it defaults to moving to the next cell (which is usually uneditable).

Here is what I'm currently doing

private final KeyStroke tabKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0);

@Override
public void changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend)
{
    AWTEvent currentEvent = EventQueue.getCurrentEvent();
    if (currentEvent instanceof KeyEvent)
    {
        KeyEvent keyEvent = (KeyEvent) currentEvent;

        if (keyEvent.getSource() == this && KeyStroke.getKeyStrokeForEvent(keyEvent).equals(tabKeyStroke))
        {
            Row nextEditableCell = getNextEditableCell(rowIndex, columnIndex);
            if (nextEditableCell != null)
            {
                rowIndex = nextEditableCell.getRow();
                columnIndex = nextEditableCell.getColumn();
            }
        }
    }
    super.changeSelection(rowIndex, columnIndex, toggle, extend);
}

How can I make my JTable, on tabs whether it is currently editing or not, shift to the next editable cell and not just the next cell?

Cogman
  • 2,070
  • 19
  • 36
  • 2
    Key bindings, key bindings, key bindings! [How to Use Key Bindings](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) – MadProgrammer Nov 18 '14 at 23:32
  • @MadProgrammer Awesome! I thought I was going down a rough path. – Cogman Nov 18 '14 at 23:35
  • 1
    Also, a "vague" [example](http://stackoverflow.com/questions/14206083/jtable-edit-on-keypress/14206486#14206486) and [example](http://stackoverflow.com/questions/23648646/use-enter-key-act-like-tab-key-on-jtable/23648996#23648996) - These demonstrate "part" of the concept you are trying to achieve, they apply key bindings to start the editing process – MadProgrammer Nov 18 '14 at 23:36

1 Answers1

0

Check out the Table Tabbing example which does what you want.

The basic concept is to customize the default tabbing Action provided by the JTable and then use Key Bindings to invoke the customized Action. The classes provided make is easy to reuse the default Actions.

camickr
  • 321,443
  • 19
  • 166
  • 288