Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Key Pressed Event

I want add a KeyEventListener to JButton which responds to Enter key, using following code segment:

   private void jButton3KeyPressed(java.awt.event.KeyEvent evt) {
        if (evt.getKeyCode() == 10) {
           eventRegister();
        }
   }                                   

I pressed space bar instead of enter and the if condition set to true and the eventRegister invoked. Why? How could I prevent this manner?

like image 517
zari Avatar asked Mar 24 '23 06:03

zari


1 Answers

  • don't to use KeyListener or MouseListener for JButton or JButtons JComponent, those events are implemented in API, or ButtonsModel, every can be testable, with to consume() of KeyEvent

  • JButton has implemented ENTER and SPACE key as accelator in KeyBindings

  • remove SPACE from KeyBindings, but not suggest that, I'm wouldn'd be confuse users, sure depends of

like image 165
mKorbel Avatar answered Mar 28 '23 18:03

mKorbel