Search code examples
javaswingoverridingactionwindowlistener

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.


Solution

  • 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()