Finding the Next Focusable Component

public Component findNextFocus() { // Find focus owner Component c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); Container root = c == null ? null : c.getFocusCycleRootAncestor(); if (root != null) { FocusTraversalPolicy policy = root.getFocusTraversalPolicy(); Component nextFocus = policy.getComponentAfter(root, c); if (nextFocus == null) { nextFocus = policy.getDefaultComponent(root); } return nextFocus; } return null; } public Component findPrevFocus() { // Find focus owner Component c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); Container root = c == null ? null : c.getFocusCycleRootAncestor(); if (root != null) { FocusTraversalPolicy policy = root.getFocusTraversalPolicy(); Component prevFocus = policy.getComponentBefore(root, c); if (prevFocus == null) { prevFocus = policy.getDefaultComponent(root); } return prevFocus; } return null; }

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.