How to make button respond to 'enter' when it has focus

J

Jim

I've several buttons on a form and can tab through them. I want to be able
to invoke a button by hitting return when it has the focus.

However, the buttons don't respond. I get action events through a keyboard
nmemonic or a mouse click but not the enter key. What object gets the enter
key action?

All I can figure out is to put a focus handler on each button and set that
button as the RootPane default button when it gets the focus and unset it
when it looses focus. Any simpler way?

Thanks,

Jim
 
C

Chris Smith

Jim said:
I've several buttons on a form and can tab through them. I want to be able
to invoke a button by hitting return when it has the focus.

However, the buttons don't respond. I get action events through a keyboard
nmemonic or a mouse click but not the enter key. What object gets the enter
key action?

All I can figure out is to put a focus handler on each button and set that
button as the RootPane default button when it gets the focus and unset it
when it looses focus. Any simpler way?

First of all, are you sure you want to do that? Normal user interface
conventions are that "enter" invokes the default button, whereas
"space" invokes the currently selected button.

If you want to change this behavior, though, you can use:

getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)

and add a mapping to an ActionMap entry to calls doClick on the selected
button.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
D

Dave Glasser

First of all, are you sure you want to do that? Normal user interface
conventions are that "enter" invokes the default button, whereas
"space" invokes the currently selected button.

Where did you learn that? I know the Metal L&F has that behavior
(which I think is stupid and counterintuitive), but in a native
Windows app, both the spacebar and the enter key will invoke the
focused button, regardless of whether it's the default button or not.
If a control that doesn't respond to the enter key, like a single-line
textfield, has the focus, then the enter key will invoke the default
button.
 
P

Peter Ashford

Dave said:
in comp.lang.java.programmer:




Where did you learn that? I know the Metal L&F has that behavior
(which I think is stupid and counterintuitive), but in a native
Windows app, both the spacebar and the enter key will invoke the
focused button, regardless of whether it's the default button or not.
If a control that doesn't respond to the enter key, like a single-line
textfield, has the focus, then the enter key will invoke the default
button.

I just tried some windows apps and they used enter for the default
button only, not the currently focused widget.
 
D

Dave Glasser

I just tried some windows apps and they used enter for the default
button only, not the currently focused widget.

When you say "focused widget," does that include focused buttons? If
not, then what you've wrote doesn't contradict what I wrote. If it
does, can you give some specific examples of apps? I tested my
hypothesis with some MS Office apps and some standard Windows dialogs,
like a file and a printer dialog, and if a button had the focus, that
button would always be invoked when the enter key was pressed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top