• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

KeyListener Problem

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm a bit baffled by my code. When the frame loads up I have to press tab once, before I can get the app to recognise a KeyEvent. Once released, I have to tab again before the key is recognised.

Ideally I would like a response, without having to stroke the tab key 1st.



I guess its something to do with the focus. I do have other components in the frame, but I want the whole thing to respond to keystrokes. I thought I'd got around this by adding the KeyAdapter to the frame.

Any suggestions?
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> I do have other components in the frame, but I want the whole thing to respond to keystrokes

this should respond to the F1 key, regardless of what component has focus

 
Jackie Davis
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for that! It works a treat.

Just so I can attempt to increase my knowledge of Java ... what are the main differences between these two methods?

Is the code you posted to with 'Key Bindings'?

I intially looked at that, but it seemed complicated.

Thanks
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> what are the main differences between these two methods?

you have added a keyListener to the frame, but it will only receive key events
if there is no focused component, or if the focused component doesn't do
anything with the key event and passes it on.

the keyEventDispatcher gets the keyEvent before it gets to the focused component

> Is the code you posted to with 'Key Bindings'?

No, explanations for key bindings here
http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html
http://java.sun.com/products/jfc/tsc/special_report/kestrel/keybindings.html
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!