1

So i have 2 components in my frame, a table and a text field. but when the table has the focus and I hit tab key, the focus doesn't go to the text field. Why this is happening?

public static void main(String[] args) {
     SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JFrame frame = new JFrame();
            String[][] data = { { "Data" } };
            String[] cols = { "COlo" };
            JTable table = new JTable(data, cols);
            table.addFocusListener(new FocusListener() {

                public void focusLost(FocusEvent arg0) {
                    System.out.println("focus lost");
                }

                public void focusGained(FocusEvent arg0) {
                    System.out.println("gained");
                }
            });
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(table, BorderLayout.NORTH);
            frame.add(new JTextField(), BorderLayout.SOUTH);
            frame.setVisible(true);
            table.requestFocus();

        }
    });
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
user2628641
  • 2,035
  • 4
  • 29
  • 45
  • 1
    This is an old article, but it might provide an explanation for what you are seeing. https://www.java.net/node/651087. Also, try searching StackOverflow for "JTable focustraversalpolicy" – terrywb Sep 05 '14 at 23:12

1 Answers1

1

Why is this happening?

Because the Tab key is used for cell-to-cell navigation in a JTable, the Focus Subsystem uses Control-Tab to navigate out of the JTable. See Customizing Focus Traversal for an example. You can alter the default behavior as shown here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045