List of utility methods to do Swing Focus
void | requestFocus(final JComponent comp) Schedules a requestFocus() call in the AWT event queue. if (comp == null) return; EventQueue.invokeLater(comp::requestFocus); |
void | requestFocus(final JComponent component) request Focus SwingUtilities.invokeLater(new Runnable() { public void run() { component.requestFocus(); }); |
void | requestFocus(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); ... |
void | requestFocusInWindow(final Component c) Executes requestFocusInWindow on the specified component using invokeLater. invokeAndWait(new Runnable() { public void run() { c.requestFocusInWindow(); }); |
void | selectAllOnFocusGained() 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); |
void | selectAndFocus(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; ... |
boolean | setFocus(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; ... |
void | setFocusableRecursively(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); |
void | setFocusOn(Component component) set Focus On final Component reference = component; SwingUtilities.invokeLater(new Runnable() { public void run() { reference.requestFocus(); }); |
void | setFocusOrder(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]); |