0

Class Web_Reader is a Swing Worker who's "doInBackground" is below. Why is "this.setProgress" not firing the "propertyChange" method? From what I have read I have implemented correctly.

   public class Web_Reader extends SwingWorker implements ACL_Form_Fetcher, PropertyChangeListener {}



    @Override
protected ACL_Form_Collection doInBackground() throws Exception {

    // setProgress(0);
    ACL_Form_Collection data = new ACL_Form_Collection();
    double numberOfForms = 0;
    ArrayList<String> input = this.readFromWeb(this.ticketNumber);
    double inputs = (double) input.size();
    while (numberOfForms <input.size() && !isCancelled()) {
        for (int i = 0; i < input.size(); i++) {
            int v = (int) (((double) numberOfForms / inputs) * 100);
            this.setProgress(v);
            Thread.sleep(5000);
            String get = input.get(i);
            //Create Form
            ACL_Form form = this.getForm(i, get);
            data.add(form);
            //Callables are added to executor and execution begins

            //  p.setNote(message);
            ++numberOfForms;

        }
        this.setProgress(100);
        //shut down executor here
    }

    return data;
}

This is the property change method located in "Web_Reader"

 @Override
public void propertyChange(PropertyChangeEvent evt) {
    System.out.println("evt= "+evt);
    if ("progress" == evt.getPropertyName()) {
        int progress = (Integer) evt.getNewValue();
        progressMonitor.setProgress(progress);
        String message = String.format("Completed %d%%.\n", progress);
        progressMonitor.setNote(message);
        System.out.println("Message= "+message);
        if (progressMonitor.isCanceled() || ACL_Maker.executor.isDone()) {
            Toolkit.getDefaultToolkit().beep();
            if (progressMonitor.isCanceled()) {
                ACL_Maker.executor.cancel(true);
                // taskOutput.append("Task canceled.\n");
            } else {
                // taskOutput.append("Task completed.\n");
            }
            //startButton.setEnabled(true);
        }
    }
}
CoupFlu
  • 311
  • 4
  • 20
  • 2
    Did you make sure to have this class add itself as a listener? – azurefrog Jan 25 '16 at 19:13
  • 1
    On an unrelated note, [don't use `==` to compare strings in Java](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java); use `String::equals()` instead. Even though in this case checking reference equality will work, it's a bad idea to rely on an implementation detail in a non-public method in this way. – azurefrog Jan 25 '16 at 20:17
  • Wouldn't implementing PropertyChangeListener and calling "this.setProgress" in the doInBackground() be enough? – CoupFlu Jan 25 '16 at 22:01
  • Nope, it just means that it is a listener. Until you tell it to listen to something, it won't. Likewise, calling `setProgress()` will fire an event to all registered listeners, but if you've never actually registered `this` as a listener, it won't be in that list. – azurefrog Jan 25 '16 at 22:15
  • That is correct. Adding this.addPropertyChangeListener(this) seems to have resolved the issue. – CoupFlu Jan 26 '16 at 14:29

0 Answers0