I am new in swing, but managed to create a descend gui.
My problem though is that I was not able to apply the patterns suggested in references/tutorials, mainly the MVC pattern.
我是新手,但设法创造了一个下降的gui。我的问题是,我无法应用参考/教程中建议的模式,主要是MVC模式。
Is it me, or in JTree and using SwingWorker, it is not possible to have a clear separation of controller/view/model?
For example I use a Swingworker, but then I can not "fit" a controller in the design.
是我,还是在JTree中使用SwingWorker,是不是可以清楚地分离控制器/视图/模型?例如,我使用Swingworker,但是我无法在设计中“适应”控制器。
I.e. the action of the control essentially is in the doBackground method that is inside e.g the action perform of a button.
So no controller class.
即控件的动作本质上是在doBackground方法中,例如在按钮的动作执行中。所以没有控制器类。
The result of the action in swing worker is to update a Jtree, so I pass the result to a class I named model, but this class has to have access to the treeModel of the Jtree that is inside the JFrame i.e. the view, so no clear separation of view and model.
swing工作器中的动作结果是更新一个Jtree,所以我将结果传递给一个名为model的类,但是这个类必须能够访问JFrame里面的jtree的treeModel,即视图,所以没有明确分离视图和模型。
I looked at a lot of tutorials but all presenting the MVC had a trivial example, and in most cases the view (which was just a some labels!) updated everything. Am I completely confused, or it is not posible or at least easy to integrate an MVC pattern in a swing application that uses swingworkers and jtrees?
我查看了很多教程,但是所有呈现MVC都有一个简单的例子,在大多数情况下,视图(只是一些标签!)更新了所有内容。我是否完全困惑,或者在使用swingworkers和jtree的swing应用程序中集成MVC模式是不可能的,或者至少是不容易的?
I am talking about the actual domain data and not the MVC that is implemented in the swing components.
我在谈论实际的域数据,而不是在swing组件中实现的MVC。
Can someone help me (and spare me from this terrible headache) either with an overview of how this design could be approached or at least with a tutorial, that is useful, with a non-trivial example?
有人可以帮助我(并避免让我摆脱这种可怕的头痛),或者概述一下如何处理这种设计,或者至少有一个教程,这是非常有用的,有一个非常重要的例子吗?
Thanks
谢谢
4 个解决方案
#1
7
When I have build larger applications (person years of development), we often abstract the MVC architecture above the individual components to a top level controller/model and view, and accept that the individual components will be their own personalized MVC. GeoffreyZheng is absolutely correct in his assessment, and it is something I have actually loved about developing with the Swing environment. That being said, if you want true MVC, you will probably need to abstract away from individual components and talk about a view in a more abstract terminology.
当我构建更大的应用程序(人员多年的开发)时,我们经常将MVC架构抽象到单个组件之上,以顶层控制器/模型和视图,并接受各个组件将是他们自己的个性化MVC。 GeoffreyZheng在他的评估中是绝对正确的,这是我真正喜欢用Swing环境开发的东西。话虽这么说,如果你想要真正的MVC,你可能需要从单个组件中抽象出来,并用更抽象的术语来讨论一个视图。
#2
5
Swing is not stricly MVC, as the company previously known as Sun openly admits it:
Swing不是严格意义上的MVC,因为该公司以前称为Sun公开承认:
(A traditional MVC separation) didn't work well in practical terms because the view and controller parts of a component required a tight coupling (for example, it was very difficult to write a generic controller that didn't know specifics about the view). So we collapsed these two entities into a single UI (user-interface) object.
(传统的MVC分离)在实际应用中效果不佳,因为组件的视图和控制器部分需要紧密耦合(例如,编写一个不了解视图细节的通用控制器非常困难) 。因此,我们将这两个实体合并为一个UI(用户界面)对象。
For JTree you have TreeModel as, you guess it, model. Some simple components like JLabel don't even have a model.
对于JTree,你有TreeModel,你猜它是模型。像JLabel这样的一些简单组件甚至没有模型。
As the link further explains, you do get certain level of separation with UI classes supplied by LAF. However Swing components themselves have to maintain and control a lot of UI related properties.
正如链接进一步解释的那样,您确实可以通过LAF提供的UI类获得一定程度的分离。但是,Swing组件本身必须维护和控制许多与UI相关的属性。
#3
4
Still in many cases it is preferable and possible: Of course the controller needs to know about the component, which is responsible for the action, but still the view doesnt need any specific action implementation. You just dont implement the action in a view class, instead you use a controller (which knows about the view and model). So add the action listener there, updating some model details for example, even within a SwingWorker.
在许多情况下,它仍然是可取的和可能的:当然,控制器需要知道负责该动作的组件,但是视图仍然不需要任何特定的动作实现。您只是不在视图类中实现该操作,而是使用控制器(它知道视图和模型)。因此,在那里添加动作侦听器,例如,甚至在SwingWorker中更新一些模型细节。
In almost every example I saw until know it was implementend like that, I dont see where this is anywhere different with any other component, like a jtree.
在我看到的几乎每个例子中,直到知道它是这样的实现,我不知道这与任何其他组件,如jtree的任何地方都不同。
Maybe with having a look at the differences between MVC and MVP (which I prefer) its easier to understand: MVC or MVP
也许看看MVC和MVP(我更喜欢)之间的差异,它更容易理解:MVC或MVP
#4
1
Not sure if this will help, but try Swing Application Framework (SAF) (JSR 296). As far as I read or try this, it helps to separate view from event handling. But I'm not so in details with more complex examples (like with JTree)
不确定这是否有帮助,但请尝试Swing应用程序框架(SAF)(JSR 296)。据我所读或尝试这一点,它有助于将视图与事件处理分开。但是我不是那么详细的例子(比如JTree)
http://java.sun.com/developer/technicalArticles/javase/swingappfr/
http://java.sun.com/developer/technicalArticles/javase/swingappfr/
https://appframework.dev.java.net/
https://appframework.dev.java.net/
Good luck!
祝你好运!
#1
7
When I have build larger applications (person years of development), we often abstract the MVC architecture above the individual components to a top level controller/model and view, and accept that the individual components will be their own personalized MVC. GeoffreyZheng is absolutely correct in his assessment, and it is something I have actually loved about developing with the Swing environment. That being said, if you want true MVC, you will probably need to abstract away from individual components and talk about a view in a more abstract terminology.
当我构建更大的应用程序(人员多年的开发)时,我们经常将MVC架构抽象到单个组件之上,以顶层控制器/模型和视图,并接受各个组件将是他们自己的个性化MVC。 GeoffreyZheng在他的评估中是绝对正确的,这是我真正喜欢用Swing环境开发的东西。话虽这么说,如果你想要真正的MVC,你可能需要从单个组件中抽象出来,并用更抽象的术语来讨论一个视图。
#2
5
Swing is not stricly MVC, as the company previously known as Sun openly admits it:
Swing不是严格意义上的MVC,因为该公司以前称为Sun公开承认:
(A traditional MVC separation) didn't work well in practical terms because the view and controller parts of a component required a tight coupling (for example, it was very difficult to write a generic controller that didn't know specifics about the view). So we collapsed these two entities into a single UI (user-interface) object.
(传统的MVC分离)在实际应用中效果不佳,因为组件的视图和控制器部分需要紧密耦合(例如,编写一个不了解视图细节的通用控制器非常困难) 。因此,我们将这两个实体合并为一个UI(用户界面)对象。
For JTree you have TreeModel as, you guess it, model. Some simple components like JLabel don't even have a model.
对于JTree,你有TreeModel,你猜它是模型。像JLabel这样的一些简单组件甚至没有模型。
As the link further explains, you do get certain level of separation with UI classes supplied by LAF. However Swing components themselves have to maintain and control a lot of UI related properties.
正如链接进一步解释的那样,您确实可以通过LAF提供的UI类获得一定程度的分离。但是,Swing组件本身必须维护和控制许多与UI相关的属性。
#3
4
Still in many cases it is preferable and possible: Of course the controller needs to know about the component, which is responsible for the action, but still the view doesnt need any specific action implementation. You just dont implement the action in a view class, instead you use a controller (which knows about the view and model). So add the action listener there, updating some model details for example, even within a SwingWorker.
在许多情况下,它仍然是可取的和可能的:当然,控制器需要知道负责该动作的组件,但是视图仍然不需要任何特定的动作实现。您只是不在视图类中实现该操作,而是使用控制器(它知道视图和模型)。因此,在那里添加动作侦听器,例如,甚至在SwingWorker中更新一些模型细节。
In almost every example I saw until know it was implementend like that, I dont see where this is anywhere different with any other component, like a jtree.
在我看到的几乎每个例子中,直到知道它是这样的实现,我不知道这与任何其他组件,如jtree的任何地方都不同。
Maybe with having a look at the differences between MVC and MVP (which I prefer) its easier to understand: MVC or MVP
也许看看MVC和MVP(我更喜欢)之间的差异,它更容易理解:MVC或MVP
#4
1
Not sure if this will help, but try Swing Application Framework (SAF) (JSR 296). As far as I read or try this, it helps to separate view from event handling. But I'm not so in details with more complex examples (like with JTree)
不确定这是否有帮助,但请尝试Swing应用程序框架(SAF)(JSR 296)。据我所读或尝试这一点,它有助于将视图与事件处理分开。但是我不是那么详细的例子(比如JTree)
http://java.sun.com/developer/technicalArticles/javase/swingappfr/
http://java.sun.com/developer/technicalArticles/javase/swingappfr/
https://appframework.dev.java.net/
https://appframework.dev.java.net/
Good luck!
祝你好运!