Hack Forums
[Java] Make thread wait for JFrame close? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Java] Make thread wait for JFrame close? (/showthread.php?tid=5047262)



Make thread wait for JFrame close? - Barinade - Nov 5, 2015

I've tried this

In main method
field - static InitThread init = new InitThread();
Code
init.start();
        synchronized (init) {
            init.wait();
            System.out.println("fin");
        }

InitThread
Code
public class InitThread extends Thread {

    @Override
    public void run() {
        synchronized (this) {
            if (settings.get("user_info", "username", String.class) == null || settings.get("user_info", "password", String.class).equals("")) {
                new LoginForm(this);
            } else {
                System.out.println("x");
                notify();
            }
        }
    }
}

But it writes "fin" before "x"
There's a button on the LoginForm that calls notify() on InitThread too

Edit: removed InitThread from the constructor of LoginForm since it's static and I can just access it from main class

Edit2: Forget this, just gonna make it run the code I want it to wait for inside the interface :/


* - matt - Nov 8, 2015

You can add a shutdown hook to your program and if you set the default close operation to 3 (exit JVM on close) it will execute the hook before the JVM exits completely