Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your current code shows no creation of a background thread, but rather it shows you trying to queue code on the Swing thread <strong><em>from within</em></strong> the Swing thread which doesn't make sense for this problem (although there are occasional times when you may want to do this, but again, not here). The only way for this to succeed is to use a background thread. The standard Oracle <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/progress.html">JProgressBar tutorial</a> and <a href="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency in Swing</a> goes through all of this. </p> <p>The basic thing is that you must update the JProgressBar from the Swing Thread will doing your long-running process in the background thread, such as that provided by a SwingWorker object. There are too many details for us to review all here, and so all I can do is provide a link, but we'll be happy to help you understand the details once you review the tutorials. Just check the tutorials and come on back with your specific questions if you're still stuck.</p> <p><strong>Edit 1</strong><br> You state: </p> <blockquote> <p>can I just create a new thread object within the buttonClicked() function? </p> </blockquote> <p>Yes, you can create a SwingWorker object inside of the <code>buttonClicked()</code> <em>method</em> and execute it there. </p> <blockquote> <p>The thing is I have my API and library of all the functionality that I'm developing the GUI to, and it seems like a longwinded workaround to wrap that function call in a thread.</p> </blockquote> <p>Sorry, but I have no idea what you're saying here or what issues you think threading will cause. The <code>buttonClicked()</code> method likely <em>must</em> run on the EDT and not in a background thread. </p> <p>Also note that in most of my more complex Swing GUI's, I often do my file downloading in a different (model) object and create my SwingWorker in a different object still (control) from the GUI object (the view). It may seem more complicated to do it this way, but it's a <strong><em>lot</em></strong> easier to debug, maintain and enhance my program when I do it this way, especially when I heavily use interfaces to allow me to test all program components in isolation.</p> <p><strong>Edit 2</strong><br> Some corrections to your solution post. You posted:</p> <pre><code>public void buttonClicked() { class MyWorker extends SwingWorker(String, Object) { protected String runInBackground() { progressBar.setVisible(true); progressBar.setIndeterminate(true); // ... </code></pre> <p>which has problems </p> <ul> <li>it's <code>doInBackground()</code>, not <code>runInBackground()</code></li> <li>but more importantly, you're making Swing calls from within a background thread, something that should never be done (unless the call is thread safe, and even then...).</li> </ul> <p>So change it:</p> <pre><code>public void buttonClicked() { progressBar.setVisible(true); progressBar.setIndeterminate(true); class MyWorker extends SwingWorker&lt;String, Void&gt; { protected String doInBackground() { // ... </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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