Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Default button  RSS feed

 
Kejal Shah
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all.
I'm developing an application in which there's a JFrame with other components in it including a JButton. What i want is that whenever the user presses the enter key, the JButton's actionevent should get fired.
Is there some way i can achieve it?
Tx in advance
Kejal
 
Ajit Malleri
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
add keyListener to your component and listen for keyPressed event. Check the keycode for enter (10) and do the needful.
 
Abhik Sarkar
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kejal,
You could also try this...
 
Thorsten Schenkel
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
the usage of
frame.getRootPane().setDefaultButton(theButtonYouWant);
to set the default button has one problem: if e.g. a tree or a table is part of the dialog or the frame and the tree or the table has the focus "return" does not activate the default button, because the tree or the table handle "return" themselves. In this case the user must use ctrl+return to activate the default button.
 
Kejal Shah
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tx to all of u. I used
component.getInputMap().put() and
component.getActionMap().put() as a solution.
Thanks anyways
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!