event listener

W

wee

hi,

i have a JButton object and i added an actionListener to it. it works
well with a mouse click, but if i use the enter key while the focus is
on the JButton, nothing happens. do i need to add a keypress listener
on top of the actionListener to the JButton so that it would work both
for a keypress and a mouse click? help on this query is greatly
appreciated.

thanks..
 
J

John B. Matthews

wee said:
I have a JButton object and I added an actionListener to it. It works
well with a mouse click, but if I use the enter key while the focus
is on the JButton, nothing happens. Do I need to add a keypress
listener on top of the actionListener to the JButton so that it would
work both for a keypress and a mouse click? Help on this query is
greatly appreciated.

The default key binding for Swing buttons is the space key [1]. You can
add a binding to the enter key, as shown here [2], but the root pane's
setDefaultButton() method [3] is a convenient alternative.

[1]<http://download.oracle.com/javase/tutorial/uiswing/misc/keybinding.html>
[2]<http://sites.google.com/site/drjohnbmatthews/keypad-panel>
[3]<http://download.oracle.com/javase/6/docs/api/javax/swing/JRootPane.html>
 

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
473,770
Messages
2,569,586
Members
45,083
Latest member
SylviaHarr

Latest Threads

Top