Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>I have read that the problem might be due to the lack of a thread that surrounds the code</p> </blockquote> <p>Would be a suitable assumption. Swing is a single threaded environment, which means, all interactions and updates to the UI are required to be made within the context of the Event Dispatching Thread. Equally in action or operation which blocks this thread will prevent the EDT from processing (amongst other things) paint requests.</p> <p>While there are a number of possible solutions, the best suited to your needs is probably the <code>SwingWorker</code>.</p> <p>This allows you to run lengthy operations in a background thread, but has a number of methods by which you can re-sync the out comes of these operations back to the EDT.</p> <p>Check out <a href="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/" rel="nofollow noreferrer">Concurrency in Swing</a> for more details.</p> <p>How you use it will depend on your needs...</p> <p>For example, you could simply "publish" the current row your are processing...</p> <pre><code>public class Generator&lt;Node , Integer&gt; { protected void process(List&lt;Integer&gt; rows) { // Back in the EDT int lastRow = rows.get(rows.size() - 1); // Only interested in the last one... // Update the UI... } protected Node doInBackground() throws Exception { Node topNode = new Node [DatabaseFile.get(0).length]; /*...*/ publish(rowNumber); /*...*/ return topNode; } public void done() { // Back in the EDT try { Node topNode = get(); // Get the result of the calculation... } catch (Exception exp) { exp.printStackTrace(); } } } </code></pre> <p>Or you could take advantage of the <code>SwingWorker</code>'s property change support and use it's in built <code>setProgress</code> method.</p> <p>See...</p> <ul> <li><a href="https://stackoverflow.com/questions/12020949/jprogressbar-isnt-progressing/12021971#12021971">JProgressBar isn&#39;t progressing</a></li> <li><a href="https://stackoverflow.com/questions/15668715/how-to-use-the-swing-timer-to-delay-the-loading-of-a-progress-bar/15669717#15669717">How to use the Swing Timer to delay the loading of a progress bar</a></li> </ul> <p>For some examples.</p> <p>These will ensure that whole you a processing your calculations, the UI remains responsive to your changes</p>
 

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