Note that there are some explanatory texts on larger screens.

plurals
  1. POWaiting for thread while updating Swing
    primarykey
    data
    text
    <p>I have problem with handling threads in my application. It creates JFrame and starts a new Thread. Last one will execute external application and update GUI. Then </p> <p>I have problem to make Main class to wait for second thread to finish, but also to update GUI simultaneously.</p> <p>Here's my example (shortened):</p> <pre><code>class Main { public int status; public Main() { // Creating GUI etc. SwingUtilities.invokeLater(new Runnable() { public void run() { JDialog id = new JDialog(); id.button.addMouseListener(new MouseListener()); // Calls generate() method } }); } public void generate() { SwingUtilities.invokeLater(new Runnable() { public void run() { // Make changes to GUI } }); GeneratorThread genTest = new GeneratorThread(this, 1, 1, 1); genTest.start(); //while (status == 0); System.out.println("Next step."); } } </code></pre> <p>And Thread class:</p> <pre><code>public class GeneratorThread extends Thread { protected Main main; protected int setSize, minValue, maxValue; public GeneratorThread(Main main, int setSize, int minValue, int maxValue) { this.main = main; this.setSize = setSize; this.minValue = minValue; this.maxValue = maxValue; } public void run() { // Execute program etc. // Change GUI from main in the same time // About 3 seconds main.status = 1; } } </code></pre> <p>I'm in progress and I wanted to check how it works so far. While worked nice, but it locks Swing somehow and any changes are visible only when <code>GeneratorThread</code> finishes. I would like to update GUI in the real time.</p> <p>I've tried <code>join()</code>, effects are the same. I also tried <code>wait()</code> (on <code>Main</code>), but then I got IllegalStateMonitorException.</p> <p>Any hints?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload