• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Understanding key bindings.

 
Michael Boehm
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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/
 
Michael Boehm
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic