Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As @happyburnout has pointed out, you'd be better of processing you download in a separate thread, using a <a href="http://docs.oracle.com/javase/7/docs/api/javax/swing/SwingWorker.html" rel="nofollow">SwingWorker</a> is probably the best solution for what you are doing.</p> <p>The main reason is you're blocking the <a href="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html" rel="nofollow">Event Dispatching Thread</a> (AKA EDT) from running, preventing any repaint requests (and other UI important things) from been processed.</p> <p>You should have a read through</p> <ul> <li><a href="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html" rel="nofollow">Concurrency in Swing</a></li> <li><a href="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html" rel="nofollow">Worker Threads and Swing Worker</a></li> </ul> <p>Now this is taken almost directly from the API docs, but gives a basic idea of a <code>SwingWoker</code> with a <code>JProgressBar</code></p> <p>The "Worker"...</p> <pre><code>public class Worker extends SwingWorker&lt;Object, Object&gt; { @Override protected Object doInBackground() throws Exception { // The download code would go here... for (int index = 0; index &lt; 1000; index++) { int progress = Math.round(((float)index / 1000f) * 100f); setProgress(progress); Thread.sleep(10); } // You could return the down load file if you wanted... return null; } </code></pre> <p>The "progress pane"</p> <pre><code>public class ProgressPane extends JPanel { private JProgressBar progressBar; public ProgressPane() { setLayout(new GridBagLayout()); progressBar = new JProgressBar(); add(progressBar); } public void doWork() { Worker worker = new Worker(); worker.addPropertyChangeListener(new PropertyChangeListener() { @Override public void propertyChange(PropertyChangeEvent evt) { if ("progress".equals(evt.getPropertyName())) { progressBar.setValue((Integer) evt.getNewValue()); } } }); worker.execute(); } } </code></pre> <p>Remember the golden rules of Swing</p> <ul> <li>Never, never, never update a UI component from any <code>Thread</code> other then the EDT</li> <li>Always perform time consuming tasks on a different <code>Thread</code></li> <li>(Something, something, something about layout managers - that's more of a personal thing ;))</li> </ul> <p>And you will have a happy and easy time with Swing :D</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