Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Swing keyboard focus



I see two ways to go if you want to dispatch all key events in one place: you
can either dominate the keyboard focus management, so only your top-level
container ever gets the focus; or you can bypass the regular AWT keyboard
event dispatch hierarchy and just filter Toolkit events in a global listener.

To dominate keyboard focus management, you can do things like setting a
custom focus traversal policy on your tabbed pane (no focus cycle down) and
setFocusable(false) on all those troublesome buttons (for instance, through a
button factory).  For keyboard focus on or after Java 1.4, I like the
tutorial at http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html.

To bypass AWT event dispatch, use Toolkit.addAWTEventListener() and put a
really fast listener in there that traps only the events you want to
intercept.  Other components that get focus will receive the events also,
but you can still centralize all your handling beyond what's done by the
component UIs.

There is of course a third way to go: reconsider your assumption that
keystroke event logic shouldn't be associated with GUI components.  I have
trouble imagining why you're taking this course, unless it's just to prevent
keystrokes from activating buttons.  Maybe all you want is to prevent your
buttons from gaining focus.

Anton Kast

On Fri, 30 Dec 2005, Rolf Howarth wrote:

Any Swing experts here? :-)

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 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.

I have solutions that sometimes work on some platforms but I've been through so many iterations of this (in one form or another the code goes back to MRJ 2.2 under Mac OS 9, and Sun have changed key handling in every JDK release since then) I can no longer see the wood for the trees! I badly need a fresh perspective on this, so if anyone has any suggestions on the best way of achieving this I'd be most grateful. (The code needs to work under Java 1.4.2 under 10.3, and 1.5 and beyond under 10.4)

Thank you, and best wishes to all for the New Year,

-Rolf
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.