Key event and focus handling



I'm trying to develop a component (based on a JTabbedPane) where I
manage ALL keyboard events for the component and all components within
it (mostly buttons and labels).

Clicking anywhere in that component (on a tab, or on a button within
the current selected tab) should give the component as a whole focus.
Pressing the Tab key or clicking the mouse outside the component should
move focus off the component and onto other components in my window
(such as text fields), but I don't want Swing or AWT to do any other
focus or key event handling for the main tab pane component.

While the tab pane component as a whole has focus I need ALL key events
to go through a single processKeyEvent method, so I can provide
consistent keyboard shortcuts throughout the component, without having
to worry about subcomponents receiving focus and doing unexpected
processing. In particular, I DON'T want individual buttons to receive
focus and be activated by the space bar, I don't want left and right
arrows to activate other tabs, and so on.

What's the simplest and most robust way to do this? Register a
KeyEventDispatcher? Write my own KeyboardFocusManager? Subclass all the
buttons and other components used in my tab pane, override
processKeyEvent and have them forward the event to a central place?
Register a keyboard action for every possible keystroke? Register an
AWTEventListener? Provide an InputContext?

There are so many possible approaches to this that I can't see the wood
for the trees! (And it doesn't help that Sun completely change the way
key event handling works in every JDK release.)

-Rolf

.


Quantcast