Ben Noland
Ben Noland

Reputation: 34938

Making a JDialog button respond to the Enter key

I have a JQueryDialog with a text field, an OK button and a cancel button.

I want to be able to hit the enter key after filling in the text fields and have it do the same action as when I click the OK button.

Upvotes: 18

Views: 10693

Answers (2)

John Yeary
John Yeary

Reputation: 1113

The code is almost correct. I would change the if comparison and use the correct method as noted below.

if (KeyEvent.VK_ENTER == event.getKeyCode()) {
                yourButton.doClick();
            }

Upvotes: 1

Jason Day
Jason Day

Reputation: 8839

In your dialog, call getRootPane().setDefaultButton(okButton).

Upvotes: 43

Related Questions