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

ComponentfocusComponentOrChild(Component c)
Puts the focus on the first component in the tree of c that can accept the focus.
return focusComponentOrChild(c, false);
ComponentfocusComponentOrChild(Component c, boolean deepest)
Puts the focus on the first component in the tree of c that can accept the focus.
final Component focusable = getFocusableComponentOrChild(c, deepest);
if (focusable != null) {
    focusable.requestFocus();
return focusable;
booleanfocusFirstFocusableChild(Container c)
focus First Focusable Child
int len = c.getComponentCount();
for (int i = 0; i < len; i++) {
    if (focusFirstFocusableComponent(c)) {
        return true;
return false;
booleanfocusFirstFocusableComponent(Component c)
focus First Focusable Component
if (c == null) {
    return false;
if (c instanceof JComponent) {
    if (((JComponent) c).isRequestFocusEnabled() && c.requestFocusInWindow()) {
        return true;
if (c.isFocusable() && c.requestFocusInWindow()) {
    return true;
if (!(c instanceof Container)) {
    return false;
return focusFirstFocusableChild((Container) c);
voidfocusLater(final Component component)
focus Later
SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
        if (component instanceof JTextComponent) {
            ((JTextComponent) component).selectAll();
        component.requestFocusInWindow();
});
voidfocusLater(final Component component)
Focus a component.
SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
        if (component instanceof JTextComponent) {
            ((JTextComponent) component).selectAll();
        component.requestFocusInWindow();
});
voidfocusOnOpen(final JComponent component)
focus On Open
component.addAncestorListener(new AncestorListener() {
    public void ancestorAdded(AncestorEvent event) {
        component.requestFocusInWindow();
        component.removeAncestorListener(this);
    public void ancestorRemoved(AncestorEvent event) {
    public void ancestorMoved(AncestorEvent event) {
...
ComponentgetFocusableComponentOrChild(Component c, boolean deepest)
Returns the first component in the tree of c that can accept the focus.
if (c != null && c.isEnabled() && c.isVisible()) {
    if (c instanceof Container) {
        Container cont = (Container) c;
        if (deepest == false) { 
            if (c instanceof JComponent) {
                JComponent jc = (JComponent) c;
                if (jc.isRequestFocusEnabled()) {
                    return jc;
...
ComponentgetFocusableComponentOrChild(Component c, boolean deepest)
Returns the first component in the tree of c that can accept the focus.
if (c != null && c.isEnabled() && c.isVisible()) {
    if (c instanceof Container) {
        Container cont = (Container) c;
        if (!deepest) { 
            if (c instanceof JComponent) {
                JComponent jc = (JComponent) c;
                if (jc.isRequestFocusEnabled()) {
                    return jc;
...
ComponentgetPermanentFocusOwner()
get Permanent Focus Owner
return FocusManager.getCurrentManager().getPermanentFocusOwner();