wood burning stoves*
The moose likes Swing / AWT / SWT and the fly likes keyboard events not handled as expected when using WHEN_ANCESTOR_OF_FOCUSED_COMPONENT Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "keyboard events not handled as expected when using WHEN_ANCESTOR_OF_FOCUSED_COMPONENT" Watch "keyboard events not handled as expected when using WHEN_ANCESTOR_OF_FOCUSED_COMPONENT" New topic
Author

keyboard events not handled as expected when using WHEN_ANCESTOR_OF_FOCUSED_COMPONENT

Tina Viessmann
Greenhorn

Joined: Jun 01, 2011
Posts: 8
Hi,

I've got an application consisting of a JPanel containing an image and some focusable controls. Initially the focusable controls are disabled so that the panel has the focus. I want the image to be zoomed in / out if a specific button is typed. The shortcuts shall be executed independent of the control that has the focus. I'm doing that using the JPanels InputMap and ActionMap.

The javadoc states for JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT:
Constant used for registerKeyboardAction that means that the command should be invoked when the receiving component is an ancestor of the focused component or is itself the focused component.


Because of that my first attempt was the following:

Contrary to the javadoc, the related actions have not been executed if the panel itself has been focused.

To get my application to work as imagined, I need to do the following:

Can anyone explain to me why my first attempt is not working as I thought it would do, please?

Thank you
Tina
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

A JPanel is not focusable by default. You probably just need to setFocusable(true).

If that's not it, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.


luck, db
There are no new questions, but there may be new answers.
Tina Viessmann
Greenhorn

Joined: Jun 01, 2011
Posts: 8
Hi,

thank you for your answer. I'm kind of irritated. If a JPanel is not focusable by default, why does WHEN_FOCUSED work?

Thank you
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

To answer that, I'd have to delve deeper into the JDK sources than I'm prepared to do.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: keyboard events not handled as expected when using WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
 
Similar Threads
getting KeyPressed for JDialog? example?
NX: How to register a key event to a frame
doing same job on two or more events
Two JPanels in a JFrame listening to keyboard events
Bind jbutton event to a keystroke