Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Pattern in Java Swing?

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

like image 473
JavaUser Avatar asked Feb 03 '26 18:02

JavaUser


2 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.

like image 150
Michael Borgwardt Avatar answered Feb 06 '26 06:02

Michael Borgwardt


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.

like image 41
duffymo Avatar answered Feb 06 '26 07:02

duffymo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!