List of utility methods to do Swing Focus
boolean | hasFocus(Component component) has Focus Component focusOwner = findFocusOwner(component);
return focusOwner != null;
|
boolean | hasFocusOwner(final Component component) Returns whether component or any of its children has focus or not. final Component focusOwner = FocusManager.getCurrentManager().getFocusOwner(); return component == focusOwner || component instanceof Container && ((Container) component).isAncestorOf(focusOwner); |
void | installDefaultFocusHandling(Container c) install focus forward and backward HashSet<KeyStroke> set = new HashSet<KeyStroke>(1); set.add(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0)); c.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, set); set = new HashSet<KeyStroke>(1); set.add(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_MASK)); c.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, set); if (c instanceof JComponent) { JComponent jc = (JComponent) c; ... |
void | installFocusCycleRoot(Container c, FocusTraversalPolicy cyclePolicy) installs a focus cycle, its keys and the (optional) policy c.setFocusCycleRoot(true); c.setFocusTraversalPolicy(cyclePolicy); if (c instanceof RootPaneContainer) { Container contentPane = ((RootPaneContainer) c).getContentPane(); if (contentPane != null) { contentPane.setFocusTraversalPolicy(null); contentPane.setFocusCycleRoot(false); |
void | installJComponentRepainterOnWindowFocusChanged(JComponent component) Installs a listener on the given JComponent 's parent Window that repaints the given component when the parent window's focused state changes. WindowListener windowListener = createWeakWindowFocusListener(createRepaintWindowListener(component)); AncestorListener ancestorListener = createAncestorListener(component, windowListener); component.addAncestorListener(ancestorListener); |
void | installJComponentRepainterOnWindowFocusChanged(JComponent component) Installs a listener on the given JComponent 's parent Window that repaints the given component when the parent window's focused state changes. WindowFocusListener windowListener = createWeakWindowFocusListener(createRepaintWindowListener(component)); AncestorListener ancestorListener = createAncestorListener(component, windowListener); component.addAncestorListener(ancestorListener); |
void | installJComponentRepainterOnWindowFocusChanged(JComponent component) Installs a listener on the given JComponent 's parent Window that repaints the given component when the parent window's focused state changes. final WindowFocusListener windowListener = createRepaintWindowListener(component); final AncestorListener ancestorListener = createAncestorListener(component, windowListener); component.addAncestorListener(ancestorListener); |
boolean | isDescendingFrom(Component focusOwner, Component parent) Returns whether the component is part of the parent's container hierarchy. while (focusOwner != null) { if (focusOwner instanceof JPopupMenu) { focusOwner = ((JPopupMenu) focusOwner).getInvoker(); if (focusOwner == null) { return false; if (focusOwner == parent) { ... |
boolean | isFocusable(Component component) Return true if the jComponent could receive the focus if (component instanceof JTextComponent || component instanceof JComboBox || component instanceof JCheckBox || component instanceof JTable) { return true; return false; |
boolean | isFocused(Component component) Returns true, if the specified component is focus owner or permanent focus owner and if the component is on an the active window. if (component instanceof JComponent) { if (((JComponent) component).getClientProperty("Quaqua.Component.cellRendererFor") != null) { component = (Component) ((JComponent) component) .getClientProperty("Quaqua.Component.cellRendererFor"); KeyboardFocusManager m = KeyboardFocusManager.getCurrentKeyboardFocusManager(); Component c = m.getPermanentFocusOwner(); ... |