List of utility methods to do Swing Focus
void | addDefaultFocus(Window window, final JComponent component) add Default Focus window.addWindowFocusListener(new WindowAdapter() { @Override public void windowGainedFocus(WindowEvent e) { component.requestFocusInWindow(); }); |
void | adjustFocus(JComponent c) Request focus on the given component if it doesn't already have it and isRequestFocusEnabled() returns true.
if (!c.hasFocus() && c.isRequestFocusEnabled()) {
c.requestFocus();
|
void | centreInFocusedWindow(Window window, int xOffset, int yOffset) centre In Focused Window Window focusedWindow = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow(); centreInContainer(focusedWindow, window, xOffset, yOffset); |
void | checkComponentsFocusable(final Component _rootComponent) Check the components. if (_rootComponent.isFocusable()) { System.out.println( "Focusable: \t" + _rootComponent.getClass().getSimpleName() + _rootComponent.getSize()); if (_rootComponent instanceof JPanel) { for (Component x : ((JPanel) _rootComponent).getComponents()) { checkComponentsFocusable(x); } else if (_rootComponent instanceof JFrame) { for (Component x : ((JFrame) _rootComponent).getContentPane().getComponents()) { checkComponentsFocusable(x); } else if (_rootComponent instanceof Panel) { for (Component x : ((Panel) _rootComponent).getComponents()) { checkComponentsFocusable(x); } else if (_rootComponent instanceof Window) { for (Component x : ((Window) _rootComponent).getComponents()) { checkComponentsFocusable(x); |
void | clearAWTFocus(Robot robot) clear AWT Focus if (null == robot) { robot = new Robot(); robot.setAutoWaitForIdle(true); javax.swing.SwingUtilities.invokeAndWait(new Runnable() { public void run() { System.err.println("******** clearAWTFocus.0"); java.awt.KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwner(); ... |
Component | compositeRequestFocus(Component component) composite Request Focus if (component instanceof Container) { Container container = (Container) component; if (container.isFocusCycleRoot()) { FocusTraversalPolicy policy = container.getFocusTraversalPolicy(); Component comp = policy.getDefaultComponent(container); if (comp != null) { comp.requestFocus(); return comp; ... |
boolean | containerContainsFocus(Container cont) containerContainsFocus, does the specified container contain the current focusOwner? Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); Component permFocusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner(); boolean focusOwned; focusOwned = ((focusOwner != null) && SwingUtilities.isDescendingFrom(focusOwner, cont)); if (!focusOwned) { focusOwned = ((permFocusOwner != null) && SwingUtilities.isDescendingFrom(permFocusOwner, cont)); return focusOwned; ... |
boolean | containerContainsFocus(Container cont) container Contains Focus Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); Component permFocusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager() .getPermanentFocusOwner(); boolean focusOwned = (focusOwner != null) && (SwingUtilities.isDescendingFrom(focusOwner, cont)); if (!focusOwned) { focusOwned = (permFocusOwner != null) && (SwingUtilities.isDescendingFrom(permFocusOwner, cont)); return focusOwned; |
FocusAdapter | createWizardFocusAdapter() create Wizard Focus Adapter return new FocusAdapter() { @Override public void focusGained(FocusEvent e) { if (e.getSource() instanceof JTextComponent) { JTextComponent tf = (JTextComponent) e.getSource(); tf.setSelectionStart(0); tf.setSelectionEnd(tf.getText().length()); }; |
boolean | focusComponent(Component comp) Tries to focus the given component. if (comp == null) { return false; if (comp.isFocusable() && !(comp instanceof JScrollPane)) { comp.requestFocus(); return true; if (comp instanceof Container) { ... |