2

Is there a way to skip focus to a particular component when we use "Tab" key. If user double clicks on the component then focus should go to that text.

user1430989
  • 525
  • 2
  • 6
  • 15
  • Look at How to use the Focus Subsystem -- http://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html – sjr Jan 04 '13 at 17:06

2 Answers2

3

So basically you want to alter JTable functionality on Tab pressed?

Swing uses KeyBindings simply replace existing functionality of Swing component on keypressed etc by adding a new KeyBinding to JTable (the beauty happens because of JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT):

table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0), "tab");
table.getActionMap().put("tab", new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent ae) {
        //do something on JTable tab pressed or do nothing
    }
});
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • 2
    While I would probably have approached this problem the same way, Kleopatra was kind enough to enlighten me with a slightly better approach. Check out [her answer](http://stackoverflow.com/questions/12154734/change-focus-to-next-component-in-jtable-using-tab), I think you'll find it very enlightening ;) – MadProgrammer Jan 04 '13 at 20:40
  • @MadProgrammer very interesting, but IMO it does the exact same thing just at a different level. It makes more sense (to me) to replace the `Keybinding`, which was added when adding focus when by `KeyBoardManager` and FocusTranversableKeys. Unless we wanted to perhpaps focus another component on tab pressed than that would be more appropriate. – David Kroukamp Jan 05 '13 at 07:17
  • 1
    Normally I would agree, but as Kleo points out, the reason tab control works the way it does in the table is because they've modified the focus management, not the key actions - so now you're fighting on two fronts - What this does is provides us with better knowledge to find better solutions into the future ;) – MadProgrammer Jan 05 '13 at 07:28
  • @MadProgrammer very true, I'll keep this in mind for future use +1 to comments and answer cited by both you and kleo – David Kroukamp Jan 05 '13 at 07:59
2

Try table_name.setFocusable(false);

Hasi007
  • 146
  • 2
  • 14