File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Confused about keyboard in Swing" Watch "Confused about keyboard in Swing" New topic

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: 147

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

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

Joined: Jun 13, 2009
Posts: 2454
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
I agree. Here's the link:
subject: Confused about keyboard in Swing
It's not a secret anymore!