Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

one key press

 
chen young
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I created a JFrame that contain JComboBox and JButton.
When the user will press ENTER action event should run , the problem is it happen only after the second press on the ENTER since the first press will remark the text in the JComboBox and the second will activate the button action event.

How can I do it with one press ?

Thanks
 
Maneesh Godbole
Saloon Keeper
Posts: 11070
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the action you want to run is the same which should be triggered on the click of the button, check out JFrame#getRootPane#setDefaultButton(JButton button)
 
chen young
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I already try this option:





 
Maneesh Godbole
Saloon Keeper
Posts: 11070
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you click the button with the mouse, does the required action take place?
 
chen young
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes
 
Maneesh Godbole
Saloon Keeper
Posts: 11070
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you try, after making it the last line of your constructor?
Just noticed you are having an action listener as well as a key listener. Why is that? If you are effectively calling the same method as defined in action performed,please take out the key listener. You don't need it.
 
chen young
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you meant to put this line : at the end of constructor ?


I add the KeyListener so I could disable/enable the button according to the text in the textfield

 
Maneesh Godbole
Saloon Keeper
Posts: 11070
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chen yan wrote:Do you meant to put this line : at the end of constructor ?


Yes. That is what I meant.
You have also added the key listener to the button!
 
chen young
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I delete this line the Key Listener and change the position but still I need to press twice on the ENTER (first press will mark the text in the field and the second press will activate the button action event).
 
Maneesh Godbole
Saloon Keeper
Posts: 11070
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the combo popup is showing, there is no way you can trigger the action for your button.
Anybody else?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
works OK using an AWTEventListener

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic