简体   繁体   中英

JTextField focus

I have a frame and a panel.Permanently I remove the panel and add another panel.After adding a new panel I need the JTextField to get focused.How can I do this?

I tried panel.requestFocus() method but it didnt work.

Example Code:

public class Test{
    public static void main(String[] args){

        JFrame frame = new JFrame();
        // ... frame options

        // MyPanel extends JPanel
        // and has a JTextField
        contentPane.add(new MyPanel());

        // Permanently I need to add another panel
        contentPane.removeAll();
        contentPane.add(new MyPanel());

    }
}

Calling panel.requestFocus() attempts to give focus to the container itself rather than on any of its child components.

Use requestFocusInWindow on the JTextField after the component has been added to the JFrame . Add an public method in MyPanel for calling this method.

Avoid using requestFocus . From the docs :

requestFocus, is discouraged because it tries to give the focus to the component's window, which is not always possible. As of JDK 1.4, you should instead use the requestFocusInWindow method, which does not attempt to make the component's window focused.

public class Test {
    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame("Focus JTextField");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                MyPanel myPanel = new MyPanel();
                frame.add(myPanel);
                frame.setVisible(true);
                frame.pack();
                myPanel.focusTextField();
            }
        });
    }
}

class MyPanel extends JPanel {
    private JTextField textField;

    public MyPanel() {
        textField = new JTextField(20);
        add(textField);
    }

    public void focusTextField() {
        textField.requestFocusInWindow();
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(300, 100);
    }
}

Add a method like this to MyPanel:

public void gainFocus() {
    tf.requestFocus();
}

Call it from the main method, or elsewhere whenever you need it to be focused.

You will need a method either to get the TextField from MyPanel , like getTextField , or a method to just directly focus on the TextField. These methods must be inside your MyPanel class.

Example method:

public class MyPanel extends JPanel {

private JTextField textField;

//your code here

    public void getTextFieldFocus() {
        textField.requestFocus();
    }

}

Then you call this getTextFieldFocus method when you need to focus.

Else, if you extract the TextField from the MyPanel class using a getTextField method, you call this when you need the focus:

panel.getTextField().requestFocus();

Use. textfield.setText(""); when you need to get the focus or try something like you will take your control to your field try

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM