Reputation: 13
i get a trouble when i write this code for a jButton to open new jfame and all that is by using the "Enter key" but it didn't work ,this the code i have writen :
if(evt.getKeyCode()==KeyEvent.VK_ENTER){
Chooser ch = new Chooser();
ch.setVisible(true);
}
So! why? Please help me and thanks.
Upvotes: 0
Views: 72
Reputation: 347194
Buttons don't need a KeyListener
(and for the most part shouldn't use them), they use an ActionListener
to respond to all activation events, including the action key (which isn't always the Enter), mouse clicks, keyboard shortcuts and programmatically triggered events, it's a much more simplified API.
See How to Use Buttons, Check Boxes, and Radio Buttons, How to Write an Action Listeners and How to Use Actions for more details
You can also set a button as the "default" button which can be activated when not focused (so long as the currently focused component doesn't use/consume the Enter key)
See JRootPane#setDefaultButton
and How to Use Root Panes for more details
Upvotes: 1
Reputation: 19
Did You remember to add Listener for jButton ? Please give more code in Your example/ Moreover... searching in google could give interesting results: KeyListener in Textfield not firing when press enter
Upvotes: 0