Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Controlling Button Focus with Arrow Keys  RSS feed

 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got a 3x3 grid of buttons and I'd like to let the user control which button has focus by using the arrow keys on the keyboard.
Is there an easy way to do this?
Needs to be done using AWT.
Thanks!
Drew
 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm making some progress with this, but I think I need to know how get the currently focused component in order to have any chance of doing this.
How do you determine this?
Drew
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll need to keep track of focus by attaching a FocusListener to each of your buttons and use that to see which button currently has the focus.
 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I used the FocusListener and I'm receiving FocusGained and FocusLost events, but how do I know which button is triggering this?
There doesn't seem to be a method that returns the component that fired the FocusEvent e in the method below?
public void focusGained(FocusEvent e){}
Regards,
Drew
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gregg Bolinger:

OK thanks, that works for getting a reference to the Button that fired the event.
Thing is, I've got these buttons in a multi dimensional array.
Is there a method that will return the buttons index position in the array once I've got a reference to it?
Thanks,
Drew
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!