aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Understanding key bindings. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Understanding key bindings." Watch "Understanding key bindings." New topic
Author

Understanding key bindings.

Michael Boehm
Ranch Hand

Joined: Jun 02, 2010
Posts: 51
The fact that the following code does not do what I expect tells me I have misunderstood or missed something when reading the tutorials on key bindings. Even worse, it tells me that the stuff I have working in another project works only by accident I create a JFrame and add a JPanel and now using the input and action maps of the panel, I want to confirm that my binding of the down-key is activated by printing to System.out when pressing the key. Nothing happens. Hints ?

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4530
    
    5

The form of getInputMap that you have used returns the input map used when the component has focus. By default a JPanel is not focusable.

You have two options; use the form of getInputMap that can return a reference to the mapo used when the component is in hte focused window, or make the panel focusable and ensure that it has focus.

More about input maps and key bindings in Rob Camick's writeup:
http://tips4java.wordpress.com/2008/10/10/key-bindings/


luck, db
There are no new questions, but there may be new answers.
Michael Boehm
Ranch Hand

Joined: Jun 02, 2010
Posts: 51
Thanks. By trying I have just made it work using the condition JPanel.WHEN_IN_FOCUSED_WINDOW and your explanation on JPanel by default not being focusable gives me the reason
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Understanding key bindings.