awt event, how to make all button listen to 3 keys

J

John_Woo

Hi,

The JButton by default is associated with the key "space bar", pressing
that key same as clicking mouse on it.

I have a swing app which has muli-panels and buttons. I'm wondering how
to make the two "Enter" keys triger button's action as well.
 
S

Steve W. Jackson

"John_Woo said:
Hi,

The JButton by default is associated with the key "space bar", pressing
that key same as clicking mouse on it.

I have a swing app which has muli-panels and buttons. I'm wondering how
to make the two "Enter" keys triger button's action as well.

The association with the "space bar" should only apply when a button has
focus. Pressing the space bar when some other component holds focus
won't invoke your button.

I believe your second item can be resolved by setting a button as
default. Every JFrame, JDialog, etc., has a JRootPane. You can then
call that item's getRootPane() method to retrieve it, and then call its
setDefaultButton(JButton) method. That causes the button to take on a
special appearance, and to respond automatically to pressing
Enter/Return while the dialog has focus.

= Steve =
 

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

No members online now.

Forum statistics

Threads
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top