views:

190

answers:

3

Hi,

Please give me an example for MVC pattern used in Java SWING package ?

+4  A: 

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.

Michael Borgwardt
You 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
Wintermute
+1  A: 

I have very good experience using Martin Fowler's Presentation Model and its Java Swing implementation.

Boris Pavlović
martin fowler uses c#, right?
Karussell
nowadays he uses mostly ruby
Boris Pavlović
+3  A: 

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.

duffymo
Michael Borgwardt