Two Laptop Bag
The moose likes Swing / AWT / SWT and the fly likes Keys for JButtons Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Keys for JButtons" Watch "Keys for JButtons" New topic

Keys for JButtons

Veri Strora

Joined: Feb 04, 2003
Posts: 27
Hi All!
I have a JButton. How can I assign an "F2" key form the KeyBoard?
(Not "alt + F2" but "F2" only).
Thanks in advance
Joe Ess

Joined: Oct 29, 2001
Posts: 9189

does setMnemonic() work? If not, I'd think you'd have to make a Component that has focus at all times (i.e. top level frame) a key listener, listen for F2 and send an ActionEvent to the button when that key is hit.

[How To Ask Questions On JavaRanch]
Veri Strora

Joined: Feb 04, 2003
Posts: 27
Thank's Joe.
setMnemonik() work well, but setMnemonik() method make available alt + F2 and not F2.
Thank You!
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

I bet you have hundreds of these little programs like this from helping people out on javaranch.
Have you ever thought about putting together a type of SWING FAQ and for each Question, you have one of these little programs?
I know we could just search the SWING forum, but knowing a search is returning some code instead of filtering through all the garbage might be nice...
I think it would be neat and very useful. Maybe even write a book. Then you could get that Author status.

GenRocket - Experts at Building Test Data
Veri Strora

Joined: Feb 04, 2003
Posts: 27
Thank You very mutch Nathan!
Very Good Example.
I agree. Here's the link:
subject: Keys for JButtons
jQuery in Action, 3rd edition