Skip to content
Advertisement

How to use a thread to run another JFrame while the main is still running

I have a jframe i want to display while my main frame is running. i want to pause my main code, until the user does the necessary actions on the other frame. I’ve read a lot of solutions but i need to see it done for my code to understand and grasp it fully. i do not want to use jdialog like I’ve seen listed as an answer before. My main goal is to understand better threading so that i can use what i learn in different cases.

With the code I’ve created, when running the thread, only just the frame loads, none of the other features are there on the frame. (the frame is simple it has a label, a list the user selects from, and a button to basically return the chosen list value.) its like the thread is cut off from completing or something.

here is my class calling the screen:

public class myThread implements Runnable {

    String result = null;

    public void run() {
        MessageScreen ms = new MessageScreen();
        ms.setVisible(true);
    }

    public String getResult() {
        return result;
    }

    public void setResult(String AS) {
        result = AS;
    }

}

in my main code, a method is called that is returning a String[] value, with this method at some point i have the following code calling the new thread to get the value necessary to return in the original main method:

    myThread mt = new myThread();
    Thread t = new Thread(mt);
    t.start();
    try {
        t.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    myreturn = new String[] {"true", mt.getResult()}; 

without listing the whole code for the second frame, when the user presses the button, and at the end of the listener tied to the button press the i want to close the frame and return a string that was selected from the list:

jf.dispose();
myt.setResult(AdminSelection);

in the frame class, i have the following instance variables declared:

String AdminSelection = null;
myThread myt; 

i hope this is enough information for someone to help me out and understand whats gone wrong here.

Advertisement

Answer

The function join() waits until the end of the run() method, when you do t.join(), your thread is already or almost ended. This is because in your run() method there is nothing that blocks the thread until the user has clicked the confirm button. And is better like this!

There is no sense to create a thread here, you should use a callback, or more generally in Java, a listener. You can take a look at Creating Custom Listeners.


But, especially if you want to pause your main code, you should use a (modal) JDialog which is made for this! Don’t try to block the UI by yourself, you could block the UI thread (handled by Swing/AWT) by mistake. Creating a JDialog is better because everything is already made for this usage on the UI thread.

Also, you must know that create a Thread is really long, use a Thread when you really need it.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement