Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes Remove Keystroke event for enter key Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Remove Keystroke event for enter key" Watch "Remove Keystroke event for enter key" New topic
Author

Remove Keystroke event for enter key

priya thomas
Greenhorn

Joined: Mar 17, 2009
Posts: 15
Hi,

I am trying to disable/remove the enter key keyEvent. I'm adding a logonPanel (not created by me) to a loginFrame which I created.
In the logonPanel class, the submit button has a KeyListener which checks for enter key and eventually triggers an Actions interface method which I have overriden.

For incorrect logins a JOptionPane window pops up and asks to retry login and then hitting enter triggers the 'YES_OPTION'. The problem is that the Enter key event triggers down to the underlying frame and onOk gets triggered again and the JOptionPane pops up again (since the username and password havent been changed yet) and this vicious cycle continues as long as i hit 'enter' or until I use my mouse to click the 'YES_OPTION'

Im pasting the getLogonPanel method from my login class:




How can I solve this problem?

Thanks

Priya
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Im pasting the getLogonPanel method from my login class:

My car won't start.

Here's the front wheel, what's the problem?
priya thomas
Greenhorn

Joined: Mar 17, 2009
Posts: 15
Hi Michael,

I thought I gave a pretty comprehensive explanation. Do you want me to explain a bit more?
priya thomas wrote:
The problem is that the Enter key event triggers down to the underlying frame and onOk gets triggered again and the JOptionPane pops up again (since the username and password havent been changed yet) and this vicious cycle continues as long as i hit 'enter' or until I use my mouse to click the 'YES_OPTION'
Priya


Thanks,

Priya
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> I thought I gave a pretty comprehensive explanation.

so did I - the car won't start - no more explanation required.

if you want me to bring in the whole car (instead of just the front wheel),
so you can see exactly what the problem is, just ask
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
In the logonPanel class, the submit button has a KeyListener which checks for enter key


Well, the Submit button should NOT be using a KeyListener. That is what an ActionListener is for. You can also add an ActionListener to the text field to trigger the ActionListener.

Read up on how the Enter Key and Button work together.

If you need further help then you need to create a SSCCE (Short, Self Contained, Compilable and Executable, Example Program), that demonstrates the incorrect behaviour.
priya thomas
Greenhorn

Joined: Mar 17, 2009
Posts: 15
Thanks Rob,

Unfortunately as I mentioned earlier, the LogonPanel class is not mine. From my login class I only have access to an interface:



Implementing the functions onOk and onCancel will allow me to achieve functionality for the Submit and Cancel buttons of my login frame.

The buttons are using an action listener and a key listener (for users who prefer using the keyboard to the mouse)



Ideally I would like to remove KeyListeners on the JOptionPane , since it is the enter key pressed on the YES_OPTION that is causing the problem, but that isnt possible.

I can understand it is difficult to understand the scenario without the SSCCE. Let me rather ask a general button question:
If you don't have access to the button event handlers for a generic class, and you know one of the button event handlers is a keylistener which checks for 'enter' key triggers, is it possible from your class to remove the 'enter' key keyEvent? (I read up a little about inputMaps and removeKeyListener() function, but I'm not sure if that is the way to go)
Also i see that we can just put e.consume() in the keyPressedEvent where KeyEvent e is the enter keyEvent, but that's no use in my case since I'm using pre-written eventHandlers.

Any advice will do thanks

Priya



Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
Both KeyboardFocusManager and AWTEventListener can consume the event prior to it getting to the component with the listener.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Remove Keystroke event for enter key