Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

keyboard events not handled as expected when using WHEN_ANCESTOR_OF_FOCUSED_COMPONENT

 
Tina Viessmann
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tina Viessmann
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To answer that, I'd have to delve deeper into the JDK sources than I'm prepared to do.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic