• Post Reply Bookmark Topic Watch Topic
  • New Topic

want panel to keep keyboard focus after button pressed

 
Doug Mudd
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks,

First posting for me. I want to have buttons that modify various parameters but also have the user able to enter ket strokes at all times. (This is for a virtual musical instrument type of thing.)

When I start it up the panel has the focus and gets key events. Then when a button is pressed the panel loses the focus and stops getting key events. What I want to do is have the panel keep the focus or get it back again programatically right away.

I have simplified my code to isolate my problem. Thanks in advance for any advice.

Doug

 
Campbell Ritchie
Marshal
Posts: 52513
118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

We usually discuss that sort of question on the Swing forum. Moving thread.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the simple solution is to make the buttons not focusable (actionListener will still fire if button clicked)

long term you might be better off using keyBindings instead of keyListener (assuming you're only listening for a small-ish number of keys)
 
Doug Mudd
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the welcome and the suggestions. I will look into making the buttons not focusable - didn't know it was possible but that sounds like a straightforward solution. I don't know anything about keybindings but I will start looking into it. Any more advice or suggestions will be appreciated.

By the way, I am listening for 18 keys (1-9, and q-p) and will have 30 or more buttons and a couple of dropdown boxes.

Thanks again.


 
Doug Mudd
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Making buttons unfocusable works perfectly - thanks! I am still looking into keybindings but it looks messy.

This is the first time I've joined a forum and so far it is working out great.

Doug
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!