Get to know JTextField inside CardLayout

I have JTextFieldinside a JPanelA, which is part CardLayout. When this A is shown, I want to automatically focus on JTextField(i.e., the cursor is blinking in the text box, so the user does not need to click on it to enable input). I tried to call the requestFocusInWindow()object JTextFieldduring initialization, but this does not work. Do I need to call this method every time A is displayed? Thank.

+3
source share
1 answer

Perhaps you can try requestFocusInWindow()when the panel is displayed?

something like that?

    jPanel.addComponentListener(new ComponentAdapter() {
        @Override 
        public void componentShown(java.awt.event.ComponentEvent e) 
        {
            jTextField.requestFocusInWindow();
        }
    });
+5
source

All Articles