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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Two key bindings for a single button" Watch "Two key bindings for a single button" New topic
Author

Two key bindings for a single button

Chris Kimball
Ranch Hand

Joined: Apr 23, 2012
Posts: 32

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

Joined: May 03, 2008
Posts: 4522
    
    5

From the InputMap, get the value associated with <Enter> and bind it to <Shift>+<Enter>. Something like


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

Joined: Apr 23, 2012
Posts: 32

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

Joined: May 03, 2008
Posts: 4522
    
    5

You probably need to use the InputMap for WHEN_IN_FOCUSED_WINDOW
Chris Kimball
Ranch Hand

Joined: Apr 23, 2012
Posts: 32

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

Joined: Apr 23, 2012
Posts: 32

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

Joined: Jun 09, 2003
Posts: 4632
> ...set as the default button,.... That is, when the button is in focus...

the two don't match - 'default button' requires no focus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Two key bindings for a single button
 
Similar Threads
Eclipse and 3-button mice
how to receive data in .jsp file from servlet in AJAX?
JOptionPane without a button
Can Indians and Americans live in peace?
null pointer exception in struts2