JAVA: JProgressbar & Files.copy With Observer and Swingworker in 1 Class

本秂侑毒 提交于 2020-01-07 03:52:11

问题


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


回答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:

  • JProgressBar isn't progressing
  • How to use jProgress bar for ProcessBuilder process?
  • how to include a progress bar in a program of file transfer using sockets in java
  • Java - global, reusable loading dialog
  • JProgressBar won't update

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 ;)



来源:https://stackoverflow.com/questions/33136498/java-jprogressbar-files-copy-with-observer-and-swingworker-in-1-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!