Java Utililty Methods Swing Focus

List of utility methods to do Swing Focus

Description

The list of methods to do Swing Focus are organized into topic(s).

Method

voidrequestFocus(final JComponent comp)
Schedules a requestFocus() call in the AWT event queue.
if (comp == null)
    return;
EventQueue.invokeLater(comp::requestFocus);
voidrequestFocus(final JComponent component)
request Focus
SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        component.requestFocus();
});
voidrequestFocus(final Window win, final Component comp)
Focuses on the specified component as soon as the window becomes active.
win.addWindowListener(new WindowAdapter() {
    public void windowActivated(WindowEvent evt) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                comp.requestFocus();
        });
        win.removeWindowListener(this);
...
voidrequestFocusInWindow(final Component c)
Executes requestFocusInWindow on the specified component using invokeLater.
invokeAndWait(new Runnable() {
    public void run() {
        c.requestFocusInWindow();
});
voidselectAllOnFocusGained()
select All On Focus Gained
PropertyChangeListener listener = new PropertyChangeListener() {
    public void propertyChange(final PropertyChangeEvent e) {
        final Object value = e.getNewValue();
        if ((value instanceof JTextField) && !(value instanceof JPasswordField)) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    ((JTextField) value).selectAll();
            });
};
KeyboardFocusManager.getCurrentKeyboardFocusManager()
        .addPropertyChangeListener(PERMANENT_FOCUS_OWNER_PROPERTY_KEY, listener);
voidselectAndFocus(JComponent component)
Select all text in a text component and focus it.
JTextComponent textComponent = null;
if (component instanceof JTextComponent) {
    textComponent = (JTextComponent) component;
if (component instanceof JComboBox) {
    Component editorComponent = ((JComboBox<?>) component).getEditor().getEditorComponent();
    if (editorComponent instanceof JTextComponent) {
        textComponent = (JTextComponent) editorComponent;
...
booleansetFocus(JComponent component)
set Focus
if (component.requestFocusInWindow()) {
    if (component instanceof JSpinner)
        component = ((JSpinner) component).getEditor();
    if (component instanceof JTextComponent) {
        JTextComponent textComponent = (JTextComponent) component;
        textComponent.setCaretPosition(textComponent.getDocument().getLength());
    return true;
...
voidsetFocusableRecursively(final JComponent component, final boolean focusable, final boolean childsOnly)
Sets focusable state of component and all of its children.
if (!childsOnly) {
    component.setFocusable(focusable);
for (int i = 0; i < component.getComponentCount(); i++) {
    if (component.getComponent(i) instanceof JComponent) {
        setFocusableRecursively((JComponent) component.getComponent(i), focusable, false);
voidsetFocusOn(Component component)
set Focus On
final Component reference = component;
SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        reference.requestFocus();
});
voidsetFocusOrder(JComponent... components)
set Focus Order
for (int i = 0; i < components.length - 1; i++) {
    setNextFocusable(components[i], components[i + 1]);
setNextFocusable(components[components.length - 1], components[0]);