Package com.bric.plaf
Class FocusArrowListener
java.lang.Object
java.awt.event.KeyAdapter
com.bric.plaf.FocusArrowListener
- All Implemented Interfaces:
KeyListener
,EventListener
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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Set
getFocusableComponents
(Component currentFocusOwner) Returns a set of all the components that can have the keyboard focus.void
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
-
Constructor Details
-
FocusArrowListener
public FocusArrowListener()
-
-
Method Details
-
keyPressed
- Specified by:
keyPressed
in interfaceKeyListener
- Overrides:
keyPressed
in classKeyAdapter
-
shiftFocus
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
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 togetFocusCycleRootAncestor()
, andgetFocusTraversalPolicy()
.(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.
-