[英]JAVA: JProgressbar & Files.copy With Observer and Swingworker in 1 Class
I wrote an aricle a couple months ago MY POST 我写了aricle几个月前我的文章
but I want to make some changes to I. Now all my code is worked out in the Swingworker class. 但是我想对我进行一些更改。现在我所有的代码都在Swingworker类中完成了。 But I want to use the MVC methode and use the observer pattern to update my variables in the view.
但是我想使用MVC方法并使用观察者模式来更新视图中的变量。
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 我最好的选择是什么
SwingWorker
provides a form of a observer pattern in the form of a PropertyChangeListener
. SwingWorker
以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 厌倦了重写
PropertyChangeListener
,我确实创建了一个简单的适配器,它提供了更直接的事件通知
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 ;) 记住,Swing基础的“侦听器”实际上是观察者模式的一种形式;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.