简体   繁体   English

如何在Java Swing中两个不同的MVC控制器之间正确通信?

[英]How to correctly communicate between two different MVC Controllers in Java Swing?

Can you explain me how a Controller can call another Controller method in a simple but still correct way? 你能解释一下Controller如何以简单但仍然正确的方式调用另一个Controller方法吗?

Please provide some code! 请提供一些代码!

background: I Have 2 different Controllers-View-Model and 2 libraries and they need to communicate between each other: 背景:我有2个不同的控制器 - 视图模型和2个库,他们需要相互通信:

  1. SettingsWindow/SettingsController/SettingsModel: Responsible for the app settings. SettingsWindow / SettingsController / SettingsModel:负责应用程序设置。 The model is a singleton because I need the settings data all over the app; 该模型是单例,因为我需要整个应用程序的设置数据;

  2. A Library for Monitoring a directory which creates an event every time a file is created in a specific directory. 用于监视目录的库,每次在特定目录中创建文件时都会创建事件。 The monitored dir path is defined in the SettingsModel; 受监控的目录路径在SettingsModel中定义; I'm using the Java 7 WatchService API for that; 我正在使用Java 7 WatchService API;

  3. A Library for Monitoring a webserver and download new files. 用于监控Web服务器和下载新文件的库。 The webserver address and the save directory are both defined in the SettingsModel; Web服务器地址和保存目录都在SettingsModel中定义; I'm using HttpsUrlConnection and a timer for that; 我正在使用HttpsUrlConnection和一个计时器;

  4. MainWindow/MainController/MainModel: Responsible for the main app window, which has a table that must be updated every time a new file is created in the monitored directory, and also everytime a file is downloaded by the above libraries. MainWindow / MainController / MainModel:负责主应用程序窗口,该窗口具有必须在每次在受监视目录中创建新文件时更新的表,以及每次上述库下载文件时。

So how do I correctly Instantiate and communicate those 4 different features? 那么如何正确实例化和传达这4种不同的功能呢?
How do their Controllers commnuicate between them, since they are all related? 他们的控制器如何在它们之间进行通信,因为它们都是相关的? How should I organize this project in terms of MVC? 我应该如何根据MVC组织这个项目?

PLEASE provide the basic MVC scaffold (skeleton) and show where I create the instances, where and how I call each others method. 请提供基本的MVC脚手架(骨架)并显示我创建实例的位置,我在哪里以及如何调用彼此方法。 I'm new to Java, specially swing and mvc. 我是Java新手,特别是swing和mvc。 I've read a lot, but I got stuck in this situation. 我已经阅读了很多,但我遇到了这种情况。

As suggested here , patterns such as model-view-controller and observer represent recurring design elements. 正如这里所建议的那样, 模型 - 视图 - 控制器观察者模式代表了重复出现的设计元素。 A simple outline may illustrate the interaction of classes, but a general solution is beyond the scope of StackOverflow. 一个简单的大纲可以说明类的交互,但一般的解决方案超出了StackOverflow的范围。

Still, it may be useful to look at how the principles might be applied. 不过,看看如何应用这些原则可能会有所帮助。 Suppose you are following the tutorial and have implemented a WatchService in a SwingWorker , as shown here . 假设你在下面的教程 ,并实施了WatchServiceSwingWorker ,如图所示这里 Now, suppose you want the main view, a JTable , to update itself in response to an arriving WatchEvent . 现在,假设您希望主视图( JTable )更新自身以响应到达的WatchEvent You would arrange for the corresponding TableModel to register itself with the worker as a PropertyChangeListener , shown here and here . 您可以安排相应的TableModel将其自身注册为PropertyChangeListener ,如此此处所示。 When the PropertyChangeEvent arrives, you update the TableModel and fire the appropriate event to update the JTable . PropertyChangeEvent到达时,您更新TableModel并触发相应的事件以更新JTable The known TableModel implementations already include the required methods. 已知的TableModel实现已包含所需的方法。

A similar approach would be used to update the table in response to data arriving from the web. 将使用类似的方法来更新表以响应来自Web的数据。

For settings, consider java.util.Preferences , mentioned here , or javax.jnlp.BasicService , discussed here . 对于设置,考虑java.util.Preferences ,提到这里 ,或javax.jnlp.BasicService ,讨论在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM