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.
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.
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.
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.
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?
I am talking about the actual domain data and not the MVC that is implemented in the swing components.
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
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.
Swing is not stricly MVC, as the company previously known as Sun openly admits it:
(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.
For JTree you have TreeModel as, you guess it, model. Some simple components like JLabel don't even have a model.
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.
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.
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.
Maybe with having a look at the differences between MVC and MVP (which I prefer) its easier to understand: MVC or MVP
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)
http://java.sun.com/developer/technicalArticles/javase/swingappfr/
https://appframework.dev.java.net/
Good luck!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With