48

I created a form using Swing in Java. In the form I have used a JTextField on which I have to set the focus whenever I press a key. How do I set focus on a particular component in Swing?

Jasperan
  • 2,154
  • 1
  • 16
  • 40
om.
  • 4,159
  • 11
  • 37
  • 36

5 Answers5

94

Would Component.requestFocus() give you what you need?

andrewdotn
  • 32,721
  • 10
  • 101
  • 130
phunehehe
  • 8,618
  • 7
  • 49
  • 79
  • 11
    FYI, the [javadocs for JComponent](http://docs.oracle.com/javase/7/docs/api/index.html) say of `requestFocus()`, "use of this method is discouraged because its behavior is platform dependent. Instead we recommend the use of [requestFocusInWindow()](http://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#requestFocusInWindow()). If you would like more information on focus, see [How to Use the Focus Subsystem](http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html), a section in The Java Tutorial." – Adam Mackler Sep 11 '14 at 14:26
30

This would work..

SwingUtilities.invokeLater( new Runnable() { 

public void run() { 
        Component.requestFocus(); 
    } 
} );
Rhea
  • 301
  • 3
  • 2
  • 1
    This one works for me, unlike the others, especially since I'm switching from one screen to another in my app, and requesting the focus when the new screen opens. – matteoh Mar 29 '19 at 11:37
15

Now that we've searched the API all we need to do is read the API.

According to the API documentation:

"Because the focus behavior of this method is platform-dependent, developers are strongly encouraged to use requestFocusInWindow when possible. "

camickr
  • 321,443
  • 19
  • 166
  • 288
5

Note that all of the above fails for some reason in a JOptionPane. After much trial and error (more than the above stated 5 minutes, anyway), here is what finally worked:

        final JTextField usernameField = new JTextField();
// ...
        usernameField.addAncestorListener(new RequestFocusListener());
        JOptionPane.showOptionDialog(this, panel, "Credentials", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, null, null);


public class RequestFocusListener implements AncestorListener {
    @Override
    public void ancestorAdded(final AncestorEvent e) {
        final AncestorListener al = this;
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                final JComponent component = e.getComponent();
                component.requestFocusInWindow();
                component.removeAncestorListener(al);
            }
        });
    }

    @Override
    public void ancestorMoved(final AncestorEvent e) {
    }

    @Override
    public void ancestorRemoved(final AncestorEvent e) {
    }
}
Marc
  • 1,812
  • 4
  • 23
  • 36
5

You can use also JComponent.grabFocus(); it is the same

Jeff_Alieffson
  • 2,672
  • 29
  • 34
  • 3
    Javadoc for [JComponent.grabFocus()](http://docs.oracle.com/javase/8/docs/api/javax/swing/JComponent.html#grabFocus--) explicitly states that this method should not be used by the client code and suggests to use `requestFocusInWindow()` method, which is already mentioned in other answers. – Oleg Estekhin May 23 '15 at 08:50