I recently added this little stone to my GUI utility class. It simply adds new keys to the same system, which changes focus [tab]:
public static void addUpDownToTraversalKeys(Component c) { addTraversalKeys(c, KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, KeyEvent.VK_DOWN); addTraversalKeys(c, KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, KeyEvent.VK_UP); } public static void addLeftRightToTraversalKeys(Component c) { addTraversalKeys(c, KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, KeyEvent.VK_RIGHT); addTraversalKeys(c, KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, KeyEvent.VK_LEFT); } public static void addTraversalKeys(Component c, int keysetId, int...keyCodes) { HashSet<AWTKeyStroke> newKeys = new HashSet<AWTKeyStroke>( c.getFocusTraversalKeys(keysetId)); for (int keyCode : keyCodes) newKeys.add(AWTKeyStroke.getAWTKeyStroke(keyCode, 0)); c.setFocusTraversalKeys(keysetId,newKeys); }
Added to my GuiUtilities class, just calling GuiUtilities.addUpDownToTraversalKeys(this); inside the frame constructor, you can use the up and down arrow keys to go through all the elements. Please note that addLeftRightToTraversalKeys() not recommended if you received text areas;)
source share