Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

KeyListener

 
Matthew Middleton
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic