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

voidaddDefaultFocus(Window window, final JComponent component)
add Default Focus
window.addWindowFocusListener(new WindowAdapter() {
    @Override
    public void windowGainedFocus(WindowEvent e) {
        component.requestFocusInWindow();
});
voidadjustFocus(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();
voidcentreInFocusedWindow(Window window, int xOffset, int yOffset)
centre In Focused Window
Window focusedWindow = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow();
centreInContainer(focusedWindow, window, xOffset, yOffset);
voidcheckComponentsFocusable(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);
voidclearAWTFocus(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();
...
ComponentcompositeRequestFocus(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;
...
booleancontainerContainsFocus(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;
...
booleancontainerContainsFocus(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;
FocusAdaptercreateWizardFocusAdapter()
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());
};
booleanfocusComponent(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) {
...