Search code examples
javaswinghsqldb

How to get the window closed event after I invoke the main method of DatabaseManagerSwing class in HSQLDB jar?


I am using HSQL in memory database in my java application. I am opening the GUI manager provided by the HSQLDB by invoking main method of DatabaseManagerSwing class. It opened successfully.

I need to register a callback or notified when the user closes the window manually. I am not able to find anything similar in the docs and after seeing the code of DatabaseManagerSwing class, I think it is not supported.

The stop method simply clear its variables and does not call any other method. As I was thinking if it supports this, it must store the callback objects somewhere and finally call them in stop method. But unfortunately, nothing.

Is there any way or a workaround to get this done ? I need to perform some action when user closes the window.


Solution

  • I added a shutdownhook so that, it runs when the program terminates. But that's not it. Make sure while starting the HSQL Database Manager, do not pass the --noexit argument. By not passing the argument, JVM will exit when you close the Database Manager window.

    By adding this

    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            // do your stuff
        }) {
        });
    

    You can do what you want to do when the window is manually closed by the user.