Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Two key bindings for a single button

 
Chris Kimball
Ranch Hand
Posts: 35
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd like to have button, set as the default button, respond to either RETURN or SHIFT-RETURN. That is, when the button is in focus, either a return or shift-return operate the button.

I'd hate to have to define an Action to do this.
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the InputMap, get the value associated with <Enter> and bind it to <Shift>+<Enter>. Something like
 
Chris Kimball
Ranch Hand
Posts: 35
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get the idea and it's neat. However,



yields

As initialized:
0 pressed SPACE
1 released SPACE
After 2 puts:
0 shift pressed SPACE
1 shift released SPACE
2 pressed SPACE
3 released SPACE

as expected, but the button still doesn't respond to shift-return. (It responds OK to return.) I don't see why the InputMap has space, rather than enter or return.

Note the button is the default button on the containing JPanel. It's working properly in that mode.

Is there a correct time to set the InputMap? I'm doing it immediately after button instantiation.

Thanks!
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably need to use the InputMap for WHEN_IN_FOCUSED_WINDOW
 
Chris Kimball
Ranch Hand
Posts: 35
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alas, all non-void arguments to getInputMap() cause the method to return null, crashing the println loop.

I've gotten the ActionMap for the button ("pressed", "released") and tried plugging actions[0] ("pressed") into the InputMap.

inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.SHIFT_MASK ), actions[0] );

where I've set the KeyStroke explicitly.

I've been working with InputMap rather than ComponentInputMap. Don't understand the difference.

Thanks,

Chris
 
Chris Kimball
Ranch Hand
Posts: 35
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apparently I needed to be working with the RootPane, as that's where the default button is connected. The following seems to work correctly"



Thanks for your useful suggestion.

Chris
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> ...set as the default button,.... That is, when the button is in focus...

the two don't match - 'default button' requires no focus
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic