• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Regarding Action and KeyEvents

 
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
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!