com.levigo.util.swing.colorpicker
Class FocusArrowListener

java.lang.Object
  extended by java.awt.event.KeyAdapter
      extended by com.levigo.util.swing.colorpicker.FocusArrowListener
All Implemented Interfaces:
KeyListener, EventListener

public class FocusArrowListener
extends KeyAdapter

This listens for arrow keys and shifts the keyboard focus accordingly. So if you press the left arrow key, the component to the left of the source component requests the focus.

This scans for the first available component whose isFocusable() method returns true. If no such component is found: nothing happens.


Constructor Summary
FocusArrowListener()
           
 
Method Summary
static Set<Component> getFocusableComponents(Component currentFocusOwner)
          Returns a set of all the components that can have the keyboard focus.
 void keyPressed(KeyEvent e)
           
static boolean shiftFocus(int dx, int dy, Component src)
          Shifts the focus in a certain direction.
 
Methods inherited from class java.awt.event.KeyAdapter
keyReleased, keyTyped
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FocusArrowListener

public FocusArrowListener()
Method Detail

keyPressed

public void keyPressed(KeyEvent e)
Specified by:
keyPressed in interface KeyListener
Overrides:
keyPressed in class KeyAdapter

shiftFocus

public static boolean shiftFocus(int dx,
                                 int dy,
                                 Component src)
Shifts the focus in a certain direction.

Parameters:
dx - the amount to increment x.
dy - the amount to increment y.
src - the source to traverse from.
Returns:
true if another component requested the focus as a result of this method. This may return false if no suitable component was found to shift focus to. (If you press the right arrow key on the right-most component, for example.)

getFocusableComponents

public static Set<Component> getFocusableComponents(Component currentFocusOwner)
Returns a set of all the components that can have the keyboard focus.

My first implementation involved of this concept simply involved asking JCompnonents if they were focusable, but in the FilledButtonTest this resulted in shifting focus to the ContentPane. Although it is technically focusable: if I used the tab key I did not get this result. So I studied the inner workings for Component.transferFocus() and ended up with a method that involved calls to getFocusCycleRootAncestor(), and getFocusTraversalPolicy().

(Also credit goes to Werner for originally tipping me off towards looking at FocusTraversalPolicies.)

Parameters:
currentFocusOwner - the current focus owner.
Returns:
all the JComponents that can receive the focus.


Copyright © 1995-2020 levigo holding gmbh. All Rights Reserved.