Note that there are some explanatory texts on larger screens.

plurals
  1. POupdating a JProgressBar while processing
    text
    copied!<p>I know the subject has already been seen on many Questions and has been answered, but still, I can't get trough it.</p> <p>I just want to <strong>update a progressBar while extracting some stuff</strong> of a large xml file. I thought it was enough to have the time-consuming loop in a different thread but ?.. All I managed to get is the progressBar either not showed at all, or updated at the end, just before it's closed.</p> <p>Instanced somewhere near the launch of the application, I have:</p> <pre><code>public class SomeClass { private SomeClass () { myXMLParser reader = new myXMLParser(); CoolStuff fromXml = reader.readTheXml(); } } </code></pre> <p>while showing and updating a JDialog with a JProgressBar:</p> <pre><code>public class LoadingDialog extends JDialog { private JProgressBar progressBar; /* ... */ public void progress() { progressBar.setValue(progressBar.getValue() + 1); } } </code></pre> <p>So I have this <em>myXMLParser</em>:</p> <pre><code>public class myXMLParser { private LoadingDialog loadingDialog = new LoadingDialog(); public CoolStuff readTheXml() { CoolStuff fromXml = new CoolStuff(); while(manyIterations) { loadingDialog.progress(); fromXml.add(some() + xml() + reading()); } return fromXml; } } </code></pre> <p>I have seen many things with <code>SwingWorker</code> and using <code>PropertyChange</code> events update the progressBar, but examples are always given <em>all-in-one</em>, with the processing and the progressbar within the same class, and with classes within classes, and since I begin in Java, I wasn't able to translate that to my situation. </p> <p>Any help ?.. Any (not too obvious) advices ?</p> <p><strong>Edit:</strong> So thanks to btantlinger it worked like that:</p> <pre><code>public class SomeClass { private SomeClass () { myXMLParser reader = new myXMLParser(); new Thread(new Runnable() { @Override public void run() { CoolStuff fromXml = reader.readTheXml(); } }).start(); } } public class LoadingDialog extends JDialog { private JProgressBar progressBar; /* ... */ public void progress() { progressBar.setValue(progressBar.getValue() + 1); } } public class myXMLParser { private LoadingDialog loadingDialog = new LoadingDialog(); public CoolStuff readTheXml() { CoolStuff fromXml = new CoolStuff(); while(manyIterations) { SwingUtilities.invokeLater(new Runnable() { public void run() { loadingDialog.progress(); } }); fromXml.add(some() + xml() + reading()); } return fromXml; } } </code></pre>
 

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