Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close window on pressing of certain key

Tags:

java

swing

I just started using java and am not very good, so if you could help me that would be awesome! I'm making a Swing application and want to close it only when a certain key is pressed. Is there a way to do this?

like image 716
Tony Avatar asked Dec 16 '22 10:12

Tony


1 Answers

Going against the other X answers here, I'm going to recommend that you not use a KeyListener but rather use key bindings. This is a higher level abstraction, and helps you avoid focus issues that come with use of KeyListeners. You can find out more about key bindings in the Swing tutorials here: How to use Key Bindings

like image 99
Hovercraft Full Of Eels Avatar answered Dec 28 '22 13:12

Hovercraft Full Of Eels