Package com.bric.plaf
Class FocusArrowListener
- java.lang.Object
-
- java.awt.event.KeyAdapter
-
- com.bric.plaf.FocusArrowListener
-
- All Implemented Interfaces:
java.awt.event.KeyListener
,java.util.EventListener
public class FocusArrowListener extends java.awt.event.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 returnstrue
. If no such component is found: nothing happens.
-
-
Constructor Summary
Constructors Constructor Description FocusArrowListener()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static java.util.Set
getFocusableComponents(java.awt.Component currentFocusOwner)
Returns a set of all the components that can have the keyboard focus.void
keyPressed(java.awt.event.KeyEvent e)
static boolean
shiftFocus(int dx, int dy, java.awt.Component src)
Shifts the focus in a certain direction.
-
-
-
Method Detail
-
keyPressed
public void keyPressed(java.awt.event.KeyEvent e)
- Specified by:
keyPressed
in interfacejava.awt.event.KeyListener
- Overrides:
keyPressed
in classjava.awt.event.KeyAdapter
-
shiftFocus
public static boolean shiftFocus(int dx, int dy, java.awt.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 java.util.Set getFocusableComponents(java.awt.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 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.
-
-