• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding Action and KeyEvents

 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can any body please tell how to add both ActionListener and KeyListener for a same button. I have already added ActionListeners. Please can anybody show me a simple example which implement this both events.
Thanks and Reagards
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by adeeb alexander:
Hi,
Can any body please tell how to add both ActionListener and KeyListener for a same button. I have already added ActionListeners. Please can anybody show me a simple example which implement this both events.
Thanks and Reagards


Why would you even want to add a keylistener to a button? You would have to first get the button in focus, then press the key,... it really doesn't make much sense. Please explain the rationale here.

It suggests to me that you really want to use a mnemonic with your button, and that you haven't yet read the Button tutorial in the Sun Swing Tutorials which describes how to do this. If you read the tutorials and the API before asking questions, you may save yourself and us a lot of time.
 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually the default key on the panel is space that is when i press the space key the button is pressed i want to change this to enter key. How?
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two ways you could try:
1 — Make the button the default button for the JRootPane. See the setDefaultButton method in the JRootPane api - the Method Detail entry points to the limitations of this approach. Also, see the second paragraph under the header "How to Use JButton Features" on How to Use Buttons, Check Boxes, and Radio Buttons
2 — You could bind the Enter key to the button. See How to Use Key Bindings for details.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic