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!

How to have a default button for a JPanel?

843805Feb 13 2006 — edited Jan 2 2008
Hi,

For a JFrame, we can set the default Button as:
frame.getRootPane().setDefaultButton(button);
This will automatically invoke the click event of the button when Enter key is pressed. I have a JButton inside a JPanel, and I want to set the default button to this JButton which is there inside a JPanel. How can I do it?

If I add the KeyListener, then, the default focus (the black colored border) is not visible (eventhough the button's click event happens when Enter key is pressed). How can I have both, the Enter key support as well as the default focus for the JButton which will be inside the JPanel.

Thank you.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 30 2008
Added on Feb 13 2006
7 comments
1,624 views