aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Pressing ENTER when Button is selected doesnt fire buttonActionPerformed... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Pressing ENTER when Button is selected doesnt fire buttonActionPerformed..." Watch "Pressing ENTER when Button is selected doesnt fire buttonActionPerformed..." New topic
Author

Pressing ENTER when Button is selected doesnt fire buttonActionPerformed...

Paul Zak
Greenhorn

Joined: Apr 11, 2009
Posts: 10
The question is above.
Well it worked untill I defined:

Now when I press ENTER when Exit button is selected it fires:


but not anymore:


So why now it doesnt work anymore?
Is pressing a button with ENTER not an action after defining ActionMap?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
possibly this
"ENTER_PRESSED"
should be just
"ENTER"

but you could make it a bit easier by doing it this way
loginDialog.getRootPane().setDefaultButton(loginButton);
Paul Zak
Greenhorn

Joined: Apr 11, 2009
Posts: 10
But with action maps I can do this:


String "ENTER_PRESSED" is just identifier for KeyStroke.
Paul Zak
Greenhorn

Joined: Apr 11, 2009
Posts: 10
Ok I solved it.
The problem was the fact that buttonActionPerformed event wasnt invoked because the one created for ActionMap was invoked firstly.
However if you want to invoke ENTER pressed event before ActionMap's event you can use buttonKeyPressed event.
The second option is to make buttonActionPerformed event invoke faster than ActionMap's event. To do this you need to change:

to this:

And there is another option. You dont define ActionMap for ENTER pressed and you keep ActionMap for ESCAPE pressed and for ENTER you define setDefaultKey() as Michael stated above.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Paul,

Please read your private messages regarding an important announcement.

Thank you,

Rob


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Paul Zak
Greenhorn

Joined: Apr 11, 2009
Posts: 10
Ok.. It isnt still solved.
Do anyone has any other ideas why AtionMap's actionPerformed() is invoked before button's actionPerformed ?
I mean the option with:

doesnt work properly. Cause it fires when button is released..
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> doesnt work properly. Cause it fires when button is released..

(KeyEvent.VK_ENTER, 0, true)
read up on what the 3rd argument does (and you probably only need to use the 2-argument method)

Paul Zak
Greenhorn

Joined: Apr 11, 2009
Posts: 10
According to javadoc third argument is:
onKeyRelease - true if the KeyStroke should represent a key release; false otherwise.
It works then not as I want. Because then button's actionPerformed is invoked first but ActionMap's actionPerformed is invoked with too much delay (when ENTER is released) causing unwanted behaviour in my form

I know the 3rd parameter but I dont know why ActionMap's actionPerformed func is invoked before button's actionPerformed, causing it to not to be invoked, when
(KeyEvent.VK_ENTER, 0) is set (without this 3rd parameter)
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> doesnt work properly. Cause it fires when button is released..

> onKeyRelease - true if the KeyStroke should represent a key release; false otherwise. It works then not as I want.

the two are mutually exclusive, so I have absolutely no idea what you're trying to do.
hopefully someone else can read your problem better than me.
Paul Zak
Greenhorn

Joined: Apr 11, 2009
Posts: 10
Ok I wil try to explain my problem again:

I have a form with 3 buttons and 2 textfields.
BUTTONS: Login, New user..., Exit.
What I want to do is to set ENTER to fire Login button no matter on which component focus is on, except two buttons New user... button and Exit button.
And also I want to set ESC to close application when pressed.
I can do this by invoking setDefaultKey() but Im just curious why the ActionMap solution works not as I want to.

I have method loginButtonActionPerformed() and exitButtonActionPerformed() and Ive put in RootPane's ActionMap of my form 'actions' like pressing ENTER key and ESC key. They are served by two methods actionPerformed. I will focus only on ENTER key. I run application and I press ENTER button when focus is in texfield, everything works fine Login button is pressed. Then I navigate with tab to Exit button and I press ENTER. Bah... it executes actionPerformed from RootPane ActionMap and NOT exitButtonActionPerformed...

And my question is why it is happening. How to make button's own actionPerformed method be invoked before the one I defined with RootPane ActionMap.
Because now when I press ENTER when focused on Exit button, it doesnt click Exit button but fires Login button.

I hope now you understand it.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Have you looked at getting the JRootPane from the JFrame and using the setDefaultButton(JButton button) method here?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
see if the combination of the 2 indicated lines achieves what you want

focus on "OK", press enter - "OK" prints
tab to "Cancel", press enter - "Cancel" prints
tab to textField, press enter - "OK" prints



if you want your action map query looked at, can you post a simple runnable example like the above
Paul Zak
Greenhorn

Joined: Apr 11, 2009
Posts: 10
Yes it works. Thanks for replying m8.
But I asked if there is any other way I can achieve this like with ActionMaps.
Sorry that Im stubborn about that but I really would like to know why my solution doesnt work (without setDefaultButton)
Thanks again for Your interest.
Paul Zak
Greenhorn

Joined: Apr 11, 2009
Posts: 10
Sorry for delay.
Here is example.


Navigate to Cancel press Enter... "OK"

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Navigate to Cancel press Enter... "OK"

I think the problem is you expect 'enter' to be the key to trigger the button with focus.
In java, it is the spacebar key that does this
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Michael Dunn wrote:> Navigate to Cancel press Enter... "OK"

I think the problem is you expect 'enter' to be the key to trigger the button with focus.
In java, it is the spacebar key that does this


Trying to remember...isn't this even platform specific? Or is it space bar on all platforms?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Trying to remember...isn't this even platform specific? Or is it space bar on all platforms?

I've only used windows, so don't really know, but this line could indicate it would be all platforms
UIManager.put("Button.defaultButtonFollowsFocus", Boolean.TRUE);
enabling 'enter' to emulate the spacebar key
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pressing ENTER when Button is selected doesnt fire buttonActionPerformed...