• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

KeyListener

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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()?
 
Every plan is a little cooler if you have a blimp. And a tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic