user2110286
user2110286

Reputation:

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

    }
}

Upvotes: 1

Views: 4699

Answers (4)

cvbattum
cvbattum

Reputation: 799

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

Upvotes: 0

Reimeus
Reimeus

Reputation: 159874

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

Upvotes: 4

Tech Nerd
Tech Nerd

Reputation: 832

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

Upvotes: 0

Stefan
Stefan

Reputation: 12462

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.

Upvotes: 0

Related Questions