Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confused about keyboard in Swing

 
Michael Boehm
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, sounds right (I think, it's been a while since I used Swing)
 
Campbell Ritchie
Sheriff
Posts: 48386
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That question would sit better on our Swing forum. Moving thread.
 
Rob Camick
Ranch Hand
Posts: 2539
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, that was useful
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic