0

I wrote an aricle a couple months agoMY POST

but I want to make some changes to I. Now all my code is worked out in the Swingworker class. But I want to use the MVC methode and use the observer pattern to update my variables in the view.

Now i have something like this

    public class CopyFrame extends JFrame {

        private JTextarea textArea;
        private JProgresbar progresbar;

        public CopyFrame(){
            progresbar = new JProgresbar
            textArea = new JTextarea();
            progresbar.setMaximum(100));
            /*in this method I added the components correctly*/ 
            addComponentsCorrecty();

            new Task().execute();
        }


        class Task extends Swingworker<Void,Void>{
            /*Dp this 10 times*/
            Files.Copy(source, destination);
            progresbar.setValue(progresbar.getValue + 10);

            String info = String.format("%s is completed", source)

        textArea.setText(textArea.getText + "\n" + info)
        }
    }

and i want to create something like this

public class CopyFrame extends JFrame {

    private JTextarea textArea;
    private JProgresbar progresbar;

    public CopyFrame(){
        progresbar = new JProgresbar
        textArea = new JTextarea();
        progresbar.setMaximum(100));
        /*in this method I added the components correctly*/ 
        addComponentsCorrecty();

        Task task = new Task();
        task.addObserver(this);
        task.execute
    }

    @Override
    public void update(){
        progresbar.setValue(progresbar.getValue +10);
        textArea.setText(textArea.getText + "\n" + task.getInfo)
    }   
}
class Task extends Swingworker<Void,Void> extends Observer{

        private String info;
        public Task(){
            doTask;
        }

        public void doTask(){
        /*Dp this 10 times*/
        Files.Copy(source, destination);
        info = String.format("%s is completed", source)
        setChanged();
        notifyObservers();
        }

        public String getInfo(){
            return info;
        }
}

How can I do this? because I can't extend from 2 classes. I want to update the progresbar from another class (TASK)

what Is my best option

Community
  • 1
  • 1
vanlooverenkoen
  • 2,121
  • 26
  • 50

1 Answers1

1

SwingWorker provides a form of a observer pattern in the form of a PropertyChangeListener.

This provides notifications about the change in state (running/done/etc) and progress changes.

For example:

Getting a bit tired of re-writing the PropertyChangeListener, I did create a simple adapter which provided more direct event notification

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.SwingWorker;

public class SwingWorkerListener<W extends SwingWorker> implements PropertyChangeListener {

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        W worker = (W) evt.getSource();
        switch (evt.getPropertyName()) {
            case "state":
                workerStateChanged(worker);
                switch (worker.getState()) {
                    case STARTED:
                        workerStarted(worker);
                        break;
                    case DONE:
                        workerDone(worker);
                        break;
                }
                break;
            case "progress":
                workerProgressUpdated(worker);
                break;
        }
    }

    protected void workerProgressUpdated(W worker) {
    }

    protected void workerStateChanged(W worker) {
    }

    protected void workerStarted(W worker) {
    }

    protected void workerDone(W worker) {
    }

}

This allows me to override the methods that I'm actually interested and reduces the amount of repeated/boiler plate code I have to keep writing

Remember, the Swing bases "listeners" are actually a form of an observer pattern ;)

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I don't get it. my propertyChange method is never called. You are working with a switch, but what is "state" and what is "progress" – vanlooverenkoen Oct 15 '15 at 10:19
  • When will the propertyChange method be called? – vanlooverenkoen Oct 15 '15 at 10:22
  • 1. Did you add a `PropertyChangeListener` to the `SwingWorker`? 2. `propertyChanged` will be called when either the `state` or `progress` properties are changed. 3. Did you compare you code with the number of examples I linked? – MadProgrammer Oct 15 '15 at 11:18
  • `state` represents one of the three valid states that the `SwingWorker` can be in, see [`SwingWorker#getState`](https://docs.oracle.com/javase/8/docs/api/javax/swing/SwingWorker.html#getState--) for more details. `progress` is the progress property of the worker, see [`SwingWorker#getProgress`](https://docs.oracle.com/javase/8/docs/api/javax/swing/SwingWorker.html#getProgress--) and [`SwingWorker#setProgress`](https://docs.oracle.com/javase/8/docs/api/javax/swing/SwingWorker.html#setProgress-int-) for more details – MadProgrammer Oct 15 '15 at 11:20