KeyPressed event in java

Depending on where you want to trap the “enter” key, you could use an ActionListener (on such components such as text components or buttons) or attach a key binding to you component This will rely on the component being focused.

Keylistener not working for JPanel

If you search this problem, you’ll see that it is asked and has been solved many times. KeyListeners need to be on the focused component to work. One solution is to give your component the focus after first making it focusable. Better by a long shot however is to use Key Bindings. Google the tutorial … Read more