File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes one key press Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "one key press" Watch "one key press" New topic
Author

one key press

chen young
Ranch Hand

Joined: Sep 09, 2005
Posts: 190
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

Joined: Jul 26, 2007
Posts: 10268
    
    8

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)


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
chen young
Ranch Hand

Joined: Sep 09, 2005
Posts: 190
I already try this option:





Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10268
    
    8

If you click the button with the mouse, does the required action take place?
chen young
Ranch Hand

Joined: Sep 09, 2005
Posts: 190
yes
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10268
    
    8

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

Joined: Sep 09, 2005
Posts: 190
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

Joined: Jul 26, 2007
Posts: 10268
    
    8

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

Joined: Sep 09, 2005
Posts: 190
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

Joined: Jul 26, 2007
Posts: 10268
    
    8

If the combo popup is showing, there is no way you can trigger the action for your button.
Anybody else?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
works OK using an AWTEventListener

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: one key press