Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Current JButton and ActionListener on 'enter' key

843806May 6 2008 — edited May 8 2008
Question 1 - How does Java know which JButton (or any object on a panel, for that matter) is currently selected?

For example, I have a 'yes/no' panel with a 'yes' button and a 'no' button. When the panel is launched, the 'yes' button is highlighted, and I can alternate between 'yes' and 'no' with the tab key. I can also use mnemonics on the 'y' key and the 'n' key. Does the panel initialise with the 'yes' button selected because the code for the 'yes' button occurs before the code for the 'no' button?

Question 2 - How do I write code that does something like "when you hit the enter key, doClick on the currently selected JButton"? At present, nothing happens when I hit the enter key. In order for this to work, it seems to me that the code needs to know which button is currently selected, hence Question 1.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 5 2008
Added on May 6 2008
8 comments
1,395 views