Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes KeyListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "KeyListener" Watch "KeyListener" New topic
Author

KeyListener

Matthew Middleton
Greenhorn

Joined: May 05, 2004
Posts: 23
I have an application that is set up like this, the main frame is a jframe, I have a menu bar on it and two jpanels on the inside of the jframe, one to hold the textfields and one to hold my buttons, previous and next. I want to add keylisteners to the application so that when you press the right arrow key it would be the same as pressing the next button and the left arrow key the same as the Previous button. What part of my app (the jframe, the buttons or the jpanels) should I add the keylisteners too?? I think I've tried them all, but it seems like it does nothing do I have to do something else since I have so many panels, add a focus to one of them, or maybe the buttons?? Any help here would be appriciated. Thank you for your time and GOD Bless.

Matthew Middleton
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Well, here is the problem. There is no such thing as a Global KeyListener in Java. The only way to handle this is to add a KeyListener to every single component in your JFrame so that every component is listening for KeyEvents.

This is a lot easier than it sounds, luckily. Take a look at this JavaWorld article for a really good and simple example.

I did some testing with it and it worked great. Something else I didn't know before reading that article and testing that is helpful is that the Arrow Keys don't register keyTyped events. Only keyPressed and keyReleased so make sure your code that handles those events are in the right KeyListener methods.


GenRocket - Experts at Building Test Data
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
In whatever code initializes the JButtons (Next, previous, etc.) you should be able to do something like this:



I've found that KeyListener's are appropriate for situations when you want to be able to handle arbitrary input, like if you're writing a text component that accepts characters of any kind. Much more commonly I use InputMaps, which are there to handle binding a specific key to an action and can easily watch all keystrokes sent to the window or to sub-components of a parent component. Check out How to Use Key Bindings in the Java Tutorial for more info.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
If you want something to play around with, try this.

Problem is it fires 2 or 3 times - I'm guessing once for each of key-Pressed/Typed/Released
(I don't have a lot of time at present to work it out)

If you can filter out the multiple firing, it could be what you're after.

Good luck

Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

So can we all agree that one downfall of Swing is that there are too many ways to do the same thing?
P. Sagdeo
Ranch Hand

Joined: Nov 13, 2003
Posts: 67
That's programming, or even math. 1 + 2 = 3, 2 + 1 =3, 3 = 2 + 1, et.

Anyway, why can't you have the listener in any component, and just requestFocus()?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: KeyListener