Please give me an example for MVC pattern used in Java SWING package ?
3 Answers
Basically, a Swing component is itself a controller, which has a reference to a view and a model.
The view is in the JComponent.ui
field that is inherited by all swing components and used by the Look&Feel mechanism to provide different visual representations of Swing components.
There are different setModel()
methods in various subclasses that use different model types such as TableModel
or ButtonModel
, which can be implemented by an application programmer to contain the actual data that the Swing UI displays and manipulates.

- 342,105
- 78
- 482
- 720
-
2You overall statement is true, and to add a bit more, most visual components--like JTable mentioned above--use an MVC design. @JavaUser Do look into the "DefaultXX" [XX=JSomethingModel, i.e. DefaultListModel] when ever you are working with Swing... It will provide a good understanding of the Model of a given Swing Class and in most cases is enough to use as _your_ model in your application – Wintermut3 Mar 27 '10 at 13:24
-
It is a pitfall to use Swing's model as the model of your application. It has no logic and really is part of the View. – flup Jan 30 '13 at 12:25
Look at javax.swing.JTable and javax.swing.table.TableModel. JTable is the View, TableModel is the Model, and the code you write with listeners and events are the Controllers that say when the View needs to be updated.

- 305,152
- 44
- 369
- 561
-
Actually, the View would be a L&F-specific subclass of BasicTableUI – Michael Borgwardt Mar 27 '10 at 15:14
I have very good experience using Martin Fowler's Presentation Model and its Java Swing implementation.

- 63,078
- 28
- 122
- 148
-
The article is language-independent, Presentation Model works great with the Swing model classes in the View. – flup Jan 30 '13 at 12:23