How to lost focus in JTable using Tab Key?

pleasure used Ask the Experts™
I display my data in a JTable. I can use arrow key to move between the rows in the JTable, use Tab to move between each cells and Ctrl+Tab to move out from the JTable.
My question is, is there anyway for me to use Tab key instead of Ctrl+Tab to move out from the JTable? if yes, how??

please advice, thanks a lot...

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

you should overwrite the default actionMap and default inputmap of the JTable. ...or not overwrite but switch the behaviuor.

JTable table = new JTable();

InputMap im = table.getInputMap();
ActionMap am = table.getActionMap();
// read actions
KeyStroke tab = KeyStroke.getKeyStroke(KeyEevent.VK_TAB, 0);
KeyStroke ctrl_tab = KeyStroke.getKeyStroke(KeyEevent.VK_TAB, java.awt.event.InputEvent.CTRL_MASK);

Object tabKey = im.get(tab);
Action tabAction = am.get(tabKey);

Object ctrl_tabKey = im.get(ctrl_tab);
Action ctrl_tabAction = am.get(ctrl_tabKey);

// switch actions
am.put(tabKey, ctrl_tabAction);
am.put(ctrl_tabKey, tabAction);

This should be all ...


thanks for the fast response...

i get null value for tabKey and ctrl_tabKey
and because of this is i got NullPointerException..

anything i have to amend??


Please dump all the contents of the inputmap and actionmap of your table to the console. They are simple hashtables, won't be hard. Post'it here or see if there is an entry for your keystroke. Remember that every actionMap and inputMap have a parentmap - which could implement the desired behaviour.

Another solution is to change the focus by yourself. Normally the JComponent implements a protected method called processKeyEvent(KeyEvent ke), which get's called first, before any registered listener is notified. Because the method is protected, can be overwrited in a child class. The solution is to overwrite'it and when the interested keys are pressed take necesary actions (request that the next focusable component after the table to receive the focus), else call super's implementation.
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.


hi Ovi..
this is really news for me.. can u please elaborate a bit what shoud i do??

thanks .. :)


hi ovi,
this all the value i got from get ActionMap.allKeys()
, is it what u asking for??
Key : selectPreviousRowExtendSelection
Key : selectLastColumn
Key : startEditing
Key : selectFirstRowExtendSelection
Key : scrollUpChangeSelection
Key : selectFirstColumn
Key : selectFirstColumnExtendSelection
Key : scrollDownExtendSelection
Key : selectLastRow
Key : scrollRightChangeSelection
Key : selectNextColumnCell
Key : cancel
Key : scrollLeftChangeSelection
Key : selectNextRowExtendSelection
Key : selectFirstRow
Key : scrollLeftExtendSelection
Key : selectNextColumn
Key : copy
Key : scrollDownChangeSelection
Key : selectLastColumnExtendSelection
Key : selectNextRowCell
Key : selectPreviousColumnCell
Key : cut
Key : selectLastRowExtendSelection
Key : selectPreviousColumn
Key : scrollUpExtendSelection
Key : selectPreviousRowCell
Key : selectAll
Key : paste
Key : selectPreviousRow
Key : selectPreviousColumnExtendSelection
Key : scrollRightExtendSelection
Key : selectNextColumnExtendSelection
Key : selectNextRow

i can't get the value for inputmap, NullPointerException thrown..

please guide..thanks.

I will try to implement a example for you soon ...


thank you very much Ovi..thanks a lot...:)
i'm here waiting for your good news..:D
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Set;

public class t extends JTable {
  public t(int r, int c) {
    super(r, c);
  private void initMaps() {
       InputMap im = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
       ActionMap am = getActionMap();
       KeyStroke tab = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0);
       im.put(tab, "focusNextComponent");
       am.put("focusNextComponent", new ForwardFocus(this));
  class ForwardFocus extends AbstractAction {
       JTable table;
       public ForwardFocus(JTable aTable) {
            table = aTable;
       public void actionPerformed(ActionEvent ae) {

  public static void main(String[] args) {
    JFrame f = new JFrame();
    Container c = f.getContentPane();
    c.setLayout(new BorderLayout());

    t test = new t(10, 10);

    JButton b1 = new JButton("B1");
    JButton b2 = new JButton("B2");

    c.add(new JScrollPane(test), BorderLayout.CENTER);
    JPanel p = new JPanel();
    c.add(p, BorderLayout.SOUTH);
    f.setSize(300, 300);


hi Ovi,
thanks for the solutions... the points belong to you..
By the way, i got another question which is i thinnk it's almost similiar to this question.. please have a look...thanks..:)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial