Note that there are some explanatory texts on larger screens.

plurals
  1. POjProgressBar update from SwingWorker
    primarykey
    data
    text
    <p>I use to monitor a long running task by updating a ProgressBar. The long running task is of course performed in a Swingworker thread.</p> <p>I used to program things like that :</p> <pre><code>public class MySwingWorkerClass extends SwingWorker&lt;Void, Void&gt; { private JProgressBar progressBar; public MySwingWorker(JProgressBar aProgressBar) { this.progressBar = aProgressBar; progressBar.setVisible(true); progressBar.setStringPainted(true); progressBar.setValue(0); } @Override public Void doInBackground() { //long running task loop { calculation(); progressBar.setValue(value); } return null; } @Override public void done() { progressBar.setValue(100); progressBar.setStringPainted(false); progressBar.setVisible(false); } } </code></pre> <p>but recently I discovered that I could do it by using the "setProgress" and defining the property change and do things like that</p> <pre><code>public class MySwingWorkerClass extends SwingWorker&lt;Void, Void&gt; { private JProgressBar progressBar; public MySwingWorker(JProgressBar aProgressBar) { addPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { if ("progress".equals(evt.getPropertyName())) { progressBar.setValue((Integer) evt.getNewValue()); } } }); progressBar.setVisible(true); progressBar.setStringPainted(true); progressBar.setValue(0); setProgress(0); } @Override public Void doInBackground() { //long running task loop { calculation(); setProgress(value); } return null; } @Override public void done() { setProgress(100); progressBar.setValue(100); progressBar.setStringPainted(false); progressBar.setVisible(false); } } </code></pre> <p>My question is : is my first code acceptable or shall I use the setProgress for anyreason ? I find the second code more complicated and in my case and don't know if there is any advantage or reason to use the second one.</p> <p>Any advise ?</p> <p><strong>EDIT</strong> Thanks for the answer. As a summary. First solution is "wrong" because of the progress bar update is performed outside the EDT. Second solution is "correct" because the progress bar update is performed inside the EDT</p> <p>Now, according to the "interesting" answer of @mKorbel in my case my calculation give results in HTML text which I "insert" (see <a href="https://stackoverflow.com/questions/8214316/java-htmldocument-insertafterend-insertafterstart-insertbeforeend-insertbefo">this link</a>). My current code is the following.</p> <p>I publish(string) and my process code looks like that</p> <pre><code>@Override protected void process(List&lt;String&gt; strings) { for (String s : strings) { try { htmlDoc.insertBeforeEnd(htmlDoc.getElement(htmlDoc.getDefaultRootElement(), StyleConstants.NameAttribute, HTML.Tag.TABLE), s); } catch (BadLocationException ex) { } catch (IOException ex) { } } } </code></pre> <p>How can I reuse @mKobel to do the same in my case. I mean he use to override table rendering in my case what renderer shall I override (jTextPane?) and how ?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    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