You wonder in your Java Swing application why after pressing the TAB key and the next button has focus, nothing happens when you press the ENTER key. Then you find out only the SPACE key works. The culprit here is the look and feel.

The ENTER key works only in the Windows look and feel. Other look and feel themes have the SPACE character as its ENTER representation. If you want to mimic the ENTER key instead of the SPACE key even though your look and feel theme is not Windows, here is how you do it.

Simulating input events in any component in Java can be done by using the Robot class. But what if I do not want to use the Robot class? Well, it is possible. The code below simulates an ENTER keypress event. The KeyEvent object created can easily be changed to cover any key event that you want. If you noticed, one of the parameters has a value of -1. This means that neither ALT, SHIFT or CONTROL is included.

Related Posts Plugin for WordPress, Blogger...