File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Understanding key bindings." Watch "Understanding key bindings." New topic

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

Joined: May 03, 2008
Posts: 5115

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:

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
I agree. Here's the link:
subject: Understanding key bindings.
jQuery in Action, 3rd edition