Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java add ActionListener to special button

Is there a way to add/override the action event of THIS button ? I couldn't find anywhere how to acces this button and override it's action. In my case I need to do this because I need to save the resources before I exit my window, and if I press the x button, It will exit automatically.

like image 935
Teo Avatar asked Mar 21 '26 20:03

Teo


2 Answers

On the JFrame:

setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

Then add a WindowsListener and in the windowClosing(WindowEvent event) methods do your job and then call dispose()

like image 98
Guillaume Polet Avatar answered Mar 23 '26 09:03

Guillaume Polet


I'd use a shut-down hook instead that way it'll save even if you close the app via another method. Looky here

like image 34
Inverted Llama Avatar answered Mar 23 '26 08:03

Inverted Llama



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!