Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's some basis for you to look at, if you can study it and understand why every piece of code is there then I think it will help. Feel free to ask questions in a comment (although I'm going to bed right now!)</p> <p><strong>Example:</strong></p> <pre><code>public class ProgressBarDemo extends JFrame { private final JProgressBar progressBar = new JProgressBar(0, 100); private int progressCounter = 0; public ProgressBarDemo() { setContentPane(progressBar); setPreferredSize(new Dimension(100, 100)); setDefaultCloseOperation(EXIT_ON_CLOSE); pack(); new Thread(new Runnable() { public void run() { while (progressCounter &lt;= 100) { SwingUtilities.invokeLater(new Runnable() { public void run() { progressBar.setValue(progressCounter++); } }); try { Thread.sleep(500); } catch (InterruptedException e) {} } } }).start(); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new ProgressBarDemo().setVisible(true); } }); } } </code></pre> <p>Two different ways to approach the problem, using SwingWorker instead:</p> <p><strong>SwingWorker Example 1:</strong></p> <pre><code> .... public ProgressBarDemo() { setContentPane(progressBar); setPreferredSize(new Dimension(100, 100)); setDefaultCloseOperation(EXIT_ON_CLOSE); pack(); SwingWorker&lt;Integer, Void&gt; worker = new SwingWorker&lt;Integer,Void&gt;() { public Integer doInBackground() { while (progressCounter &lt;= 100) { setProgress(progressCounter++); try { Thread.sleep(500); } catch (InterruptedException e) {} } return 0; } }; worker.addPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent event) { if ("progress".equals(event.getPropertyName())) { progressBar.setValue((Integer)event.getNewValue()); } } }); worker.execute(); } .... </code></pre> <p><strong>SwingWorker Example 2 (not so nice, but interesting nonetheless):</strong></p> <pre><code> .... public ProgressBarDemo() { setContentPane(progressBar); setPreferredSize(new Dimension(100, 100)); setDefaultCloseOperation(EXIT_ON_CLOSE); pack(); new SwingWorker&lt;Integer,Integer&gt;() { public Integer doInBackground() { while (progressCounter &lt;= 100) { publish(progressCounter++); try { Thread.sleep(500); } catch (InterruptedException e) {} } return 0; } public void process(List&lt;Integer&gt; progresses) { Integer maxProgress = null; for (int progress : progresses) { if (maxProgress == null || progress &gt; maxProgress) { maxProgress = progress; } } progressBar.setValue(maxProgress); } }.execute(); } .... </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