MVC - 具有长时间运行的进程的SwingWorker应该更新视图 [英] MVC - SwingWorker with a long running process that should update the view

查看:146
本文介绍了MVC - 具有长时间运行的进程的SwingWorker应该更新视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果使用具有长时间运行的进程的SwingWorker应该将更新发送回控制器,如何获取View模式的分离?


  • 我可以使用 SwingWorkers doInBackground()保留EDT通过调用例如 model.doLongProcess()从那里很好!

  • I can use the SwingWorkers doInBackground() to keep the EDT responsive by calling e.g model.doLongProcess() from in there great!

我遇到的问题是尝试在流程完成之前获取数据,使用进度更新视图。

The issue I have is trying to get data back before the process is finished, to update the view with the progress..

我知道我可以通过使用 SwingWorkers publish()方法,但我认为强迫我写代码为 doLongProcess()方法在 doInBackground()

I know that I can get data back by using by using the SwingWorkers publish() method but this I think forces me to write the code for the doLongProcess() method within doInBackground().

为了参考MVC实现,我看起来有点像这样:

For reference the MVC implementation I have a looks a little like this:

http: //www.leepoint.net/notes-java/GUI/structure/40mvc.html

/ structure/calc-mvc/CalcMVC.java -- Calculator in MVC pattern.
// Fred Swartz -- December 2004

import javax.swing.*;

public class CalcMVC {
    //... Create model, view, and controller.  They are
    //    created once here and passed to the parts that
    //    need them so there is only one copy of each.
    public static void main(String[] args) {

        CalcModel      model      = new CalcModel();
        CalcView       view       = new CalcView(model);
        CalcController controller = new CalcController(model, view);

        view.setVisible(true);
    }
}

我有一个模型类,我们真的不想将所有这些类中的所有代码/某些/任何代码从控制器转移到控制器中。

I have one Model Class which wraps a number of other classes together to a form simple interface for the controller.

- 它不属于那里。

更新:

这是我采取的方法 - 它不是最干净的解决方案,它可以被认为是滥用 PropertyChangeSupport ..在语义上级别。

Here is the approach that I am taking - Its not the cleanest solution and It could be perceived as an abuse of PropertyChangeSupport.. on a semantic level.

基本上所有具有长时间运行方法的低级类都将有一个 propertyChangeSupport 字段。长时间运行的方法定期调用 firePropertyChange()来更新方法的状态,而不一定报告属性的更改 - 这就是我的意思是语义滥用

Basically all the low-level classes that have long running methods will have a propertyChangeSupport field. The long running methods call the firePropertyChange() periodically to update on the status of the method and not necessarily to report the change of a property - that is what I mean by semantic abuse!.

然后,包含低级别类的Model类捕获这些事件并发出自己的高级 firePropertyChange 。 。控制器可以收听...

Then the Model class which wraps the low level classes catches these events and issues its own highlevel firePropertyChange .. which the controller can listen for...

编辑:

为了澄清,当我调用firePropertyChange(propertyName,oldValue,newValue);

To clarify, when I call firePropertyChange(propertyName, oldValue, newValue);


  • propertyName --->我滥用propertyName来表示主题名称

  • oldValue = null

  • newValue =要广播的消息

然后模型中的PropertyChangeListener或者可以根据主题名称识别消息。

Then the PropertyChangeListener in the model or where ever can discern the message based on the topicname.

所以我基本上使系统像发布订阅一样使用它。

So Iv basically bent the system to use it like a publish-subscribe ....

我想代替上面的方法,我可以添加一个进度字段到低级类更新,然后firePropertyChange基于...这将与它应该是如何

I guess in place of the above method I could add a progress field to the lowlevel classes that gets updated, and then firePropertyChange based on that.. this would fall into line with how its supposed to be used.

推荐答案

我认为发布/流程对将数据从SwingWorker推送到GUI。传递信息的另一种方法是通过使用PropertyChangeSupport和PropertyChangeListeners,使GUI或控制器从SwingWorker中拉出信息。考虑

I think of the publish/process pair as pushing data from the SwingWorker into the GUI. Another way to pass information is by having the GUI or control pull the information out of the SwingWorker by using PropertyChangeSupport and PropertyChangeListeners. Consider


  • 给您的模型一个PropertyChangeSupport字段,

  • 给它添加和删除PropertyChangeListener方法<

  • 让SwingWorker向模型添加一个PropertyChangeListener。


  • <然后让SwingWorker通知控件或查看模型状态的变化。

  • SwingWorker甚至可以使用发布/进程与模型中更改的信息。

  • giving your model a PropertyChangeSupport field,
  • Giving it add and remove PropertyChangeListener methods
  • Having it notify the support object of changes in state.
  • Having the SwingWorker add a PropertyChangeListener to the model.
  • Then having the SwingWorker notifying control or view of changes in the model's state.
  • The SwingWorker could even use publish/process with the changed information from the model.

修改

关于您的更新:

Edit
Regarding your update:


基本上所有具有长时间运行方法的低级类都将具有一个propertyChangeSupport字段。长时间运行的方法定期调用firePropertyChange()来更新方法的状态,而不一定报告属性的更改 - 这就是我的意思是语义滥用!

Basically all the low-level classes that have long running methods will have a propertyChangeSupport field. The long running methods call the firePropertyChange() periodically to update on the status of the method and not necessarily to report the change of a property - that is what I mean by semantic abuse!.

我不建议你这样做。了解如果正在侦听的绑定属性不更改,即使调用firePC(),也不会通知PropertyChangeListeners(PCL)。如果您需要轮询财产,那么我不会使用PCL来做到这一点。我只是轮询它,可能是从正在轮询的班级外面。

I don't recommend that you do this. Understand that if the bound property being listened to does not change, none of the PropertyChangeListeners (PCLs) will be notified even if firePC() is called. If you need to poll a property, then I wouldn't use a PCL to do this. I would simply poll it, probably from outside of the class being polled.

这篇关于MVC - 具有长时间运行的进程的SwingWorker应该更新视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆