This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Confused about keyboard in Swing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Confused about keyboard in Swing" Watch "Confused about keyboard in Swing" New topic
Author

Confused about keyboard in Swing

Michael Boehm
Ranch Hand

Joined: Jun 02, 2010
Posts: 51
I have a program where a list of certain objects is constructed from a file. A graphical representation of the frist object in the list is drawn inside a JPanel. Now I would like to navigate the list using the keyboard, for instance the up and down arrows, and I am reading up on KeyListener and keybindings. I am a little confused. Is it correct that you use keybindings when you already have some menu/mouse action in place and just want to use keyboard as shortcut while KeyListener is for when you start from bottom like in my case ?
Jan Hoppmann
Ranch Hand

Joined: Jul 19, 2010
Posts: 143

Yup, sounds right (I think, it's been a while since I used Swing)


Life is full of choices. Sometimes you make the good ones, and sometimes you have to kill all the witnesses.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
That question would sit better on our Swing forum. Moving thread.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2180
    
    7
A Key Binding is used when you want to listen for a specific KeyStroke as oppose to any KeyStroke. So listening for up/down/left/right are specific KeyStrokes. If you want to listen for a large group of KeyStroke, say all the letters of the alphabet, then you would probably use a KeyListener rather than writing 26 key bindings.

So, in general you should always use Key Bindings. Swing was designed to use Key Bindings.

Old AWT applications used KeyListeners because Key Bindings didn't exist back then.
Michael Boehm
Ranch Hand

Joined: Jun 02, 2010
Posts: 51
Thank you, that was useful
 
wood burning stoves
 
subject: Confused about keyboard in Swing