Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You state:</p> <blockquote> <p>but when I put setProgess() inside the method of another class it tells me that I need to make that method. </p> </blockquote> <p>This has nothing to do with SwingWorker and all to do with basic Java. No class can call another classes instance (non-static) methods without doing so on a valid instance of the class. For your code to work, the "other" method must call <code>setProgress(...)</code> on an instance of the SwingWorker, and so you must pass a reference of the SwingWorker to that other class. So pass the SwingWorker (<code>this</code> inside of the SwingWorker) into the other class, and then you can happily call its methods. Again, this has nothing to do with threading, Swing, or SwingWorkers, but rather is basic bread and butter Java.</p> <p>Edit: since setProgress is protected and final, your SwingWorker will have to have a public method that the other class can call, and in this method the SwingWorker will need to call its own <code>setProgress(...)</code> method.</p> <p>e.g.,</p> <p>MyWorker class</p> <pre><code>public class MyWorker extends SwingWorker&lt;Integer, Integer&gt; { private OtherClass otherClass; public MyWorker() { otherClass = new OtherClass(this); } @Override protected Integer doInBackground() throws Exception { otherClass.otherMethod(); return null; } // public method that exposes setProgress public void publicSetProgress(int prog) { setProgress(prog); } } </code></pre> <p>OtherClass:</p> <pre><code>class OtherClass { MyWorker myWorker; public OtherClass(MyWorker myWorker) { this.myWorker = myWorker; } public void otherMethod() { for (int i = 0; i &lt; 100; i++) { myWorker.publicSetProgress(i); try { Thread.sleep(200); } catch (InterruptedException e) {} } } } </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