0

I am working on a project and until now for my frame I had setundecorated(true) to hide the above title bar and created a button to close the application because I wanted to call some methods before the application is closed.

But that doesn't help me when it is closed from the taskbar or some other way other than the button.

My question is how can I call some functions before my application is closed anyhow (from taskbar, taskmanager, from the close button in title bar or any possible way).

zappee
  • 20,148
  • 14
  • 73
  • 129
Nnnnn
  • 133
  • 3
  • 15

1 Answers1

1

Did you try shutdown hook?

public void addShutdownHook(Thread hook);

Example:

// register 'do somethings' as shutdown hook
 Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
    public void run() {
       //do somethings
    }
 }, "Program existing..."));

Javadoc

nguyentt
  • 649
  • 6
  • 13
  • where do i add this code?? sorry for asking this ridiculous question – Nnnnn Aug 09 '18 at 09:31
  • @Nnnnn: This hook works like a listener when the shutdown process is invoked. So, you can add it at the begining or somewhere in the middle of your code. – nguyentt Aug 09 '18 at 09:58