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

booleanisFocused(Component component, boolean recoursive)
is Focused
if (component.isFocusOwner())
    return true;
if (recoursive && component instanceof JComponent) {
    JComponent parentComponent = (JComponent) component;
    for (Component childComponent : parentComponent.getComponents()) {
        if (isFocused(childComponent, recoursive)) {
            return true;
return false;
booleanisParentWindowFocused(Component component)
is Parent Window Focused
Window window = SwingUtilities.getWindowAncestor(component);
return window != null && window.isFocused();
booleanisParentWindowFocused(Component component)
true if the given Component 's parent Window is currently active (focused).
Window window = SwingUtilities.getWindowAncestor(component);
return window != null && window.isFocused();
booleanisParentWindowFocused(Component component)
true if the given Component 's has a parent Window (i.e.
final Window window = SwingUtilities.getWindowAncestor(component);
return window != null && window.isFocused();
voidpaintFocusRect(Graphics g, Rectangle r)
paint Focus Rect
BasicGraphicsUtils.drawDashedRect(g, r.x - 1, r.y, r.width + 2, r.height);
voidpostponeFocus(final Component target)
Setta il focus su form modali
SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        target.dispatchEvent(new FocusEvent(target, FocusEvent.FOCUS_GAINED));
});
voidremoveFocus(JComponent component)
Makes sure the component won't receive the focus.
component.setRequestFocusEnabled(false);
component.setFocusable(false);
voidrequestComponentFocus(final Window win, final JComponent comp)
Registers a (temporary) WindowListener with the window that will activate the passed component as soon as the windowActivated() event is received.
win.addWindowListener(new WindowAdapter() {
    @Override
    public void windowActivated(WindowEvent evt) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                comp.requestFocus();
        });
        win.removeWindowListener(this);
});
voidrequestComponentFocus(final Window win, final JComponent comp)
Registers a (temporary) WindowListener with the window that will activate the passed component as soon as the windowActivated() event is received.
win.addWindowListener(new WindowAdapter() {
    @Override
    public void windowActivated(WindowEvent evt) {
        EventQueue.invokeLater(comp::requestFocus);
        win.removeWindowListener(this);
});
voidrequestFocus(final JComponent comp)
Schedules a requestFocus() call in the AWT event queue.
if (comp == null)
    return;
EventQueue.invokeLater(new Runnable() {
    @Override
    public void run() {
        comp.requestFocus();
});
...