user2628641
user2628641

Reputation: 2164

tab key can't transfer focus for JTable

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();

        }
    });
}

Upvotes: 1

Views: 741

Answers (1)

trashgod
trashgod

Reputation: 205875

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.

Upvotes: 1

Related Questions