February 27, 2021
Hot Topics:

Creating Interactive GUIs with Swing's MVC Architecture

  • By Vlad Kofman
  • Send Email »
  • More Articles »

Model Layer

The ability to separate presentation from data also lets developers modify the presentation of the same data. Therefore, issues such as sorting of filtering of pre-fetched data become very easy to solve and do not require re-fetching of the data on every user requested change in the view. You create one model object and install it into one (or many) view object(s), but you can also subclass the Model object and create your own custom models. Swing provides an abstract model class and a default concrete model for every visual component. See Listing 1 for a table of presentation classes and their corresponding models. Models usually have methods to get and set data and some specific methods correlating to the functionality of the view class, such as getColumnCount in the JTable model class. Therefore, the abstract model class only defines these required methods and the concrete one actually has their implementations (it extends the abstract class, as you probably have guessed).

For instance, the JTable view class has AbstractTableModel and DefaultTableModel. If you want to create a custom model for your JTable, with custom table header columns and their names, you can subclass DefaultTableModel and overwrite only the methods relating to the header info. See Listing 2.

You also can extend AbstractTableModel and implement all methods yourself. DefaultTableModel keeps data in a Vector of Vectors data structure, so you'll need to create a compatible memory storage. The same is true for every default Swing model; they all keep data in some kind of generic collection class. All models also have methods to get data and in some cases its index position, if it's applicable.

JList and JTable both have methods to insert and access data based on the index within the internal memory storage. This is a very important feature because it can be used to manipulate the presentation without changing the underling data.

The trick is to use three fundamental features of the Java language: inheritance, passing objects by reference, and event/listener mechanism.

Models in Swing have no information of any views; therefore, if you use a custom model, any changes in its data are conveyed to the view by an event/listener mechanism. Because Java passes all complex objects by reference, you can create a chain of custom models, all containing a reference to one main model with data and their own indexes of that data. The custom models would inherit all required methods and overwrite index methods. In the event that a user requests a change in the view, an index would be recalculated and an action event would be propagated to all interested models. This would result in a change in the view but not the model! (Please see the second part of this article for a full implementation of this technique.)

Presentation and View Layer

The view layer is what the end user sees on the screen; because we already know that its data (what is represents) is separated, the Swing development team was kind enough to make it interchangeable and even extendable to custom looks-and-feels. This is completely different from the method I've just described to change data presentation. It is an application-specific GUI modification and not data specific. What that means is that you can switch the way your GUI looks, make a Java application look like a Solaris Motif program on Windows XP, for instance. Code like this (before any Swing components' initialization) would do the trick:

try {
} catch...

And, even though your JTextArea and JTree would look different, it would still contain the same data and work in the same way; you can still use the same data-specific filtering and sorting techniques. The mechanism by which it works is called a Pluggable Look and Feel (PLAF) and is beyond the scope of this article, but you can find a great tutorial on Sun Microsystems' site about PLAF (see "A Swing Architecture Overview" in the references section).


In this part, I've described the fundamentals of Swing separable model architecture. I've gone over the structure of Model objects and their correlation to the View objects, and the way you can install custom models into your UI components. I also briefly described how the power of separation can let developers change the presentation without touching the corresponding information, both by means of event mechanism and by the Pluggable Look and Feel feature of Swing. By using custom models, you can achieve great flexibility and superior user functionality, at the same time avoiding unnecessary requests of data, caching, or other methods of altering presentation.

The included source code contains the full JTable example from Part 2 of this article with several custom models to filter and sort data.

Source Code

Download the source code here.

Listing 1

Component Model Interface Model Type
JButton ButtonModel GUI
JToggleButton ButtonModel GUI/data
JCheckBox ButtonModel GUI/data
JRadioButton ButtonModel GUI/data
JMenu ButtonModel GUI
JMenuItem ButtonModel GUI
JCheckBoxMenuItem ButtonModel GUI/data
JRadioButtonMenuItem ButtonModel GUI/data
JComboBox ComboBoxModel data
JProgressBar BoundedRangeModel GUI/data
JScrollBar BoundedRangeModel GUI/data
JSlider BoundedRangeModel GUI/data
JTabbedPane SingleSelectionModel GUI
JList ListModel data
JList ListSelectionModel GUI
JTable TableModel data
JTable TableColumnModel GUI
JTree TreeModel data
JTree TreeSelectionModel GUI
JEditorPane Document data
JTextPane Document data
JTextArea Document data
JTextField Document data
JPasswordField Document data

Listing 2

class PlanetTableModel extends DefaultTableModel {
   private static String[] planetModelNames
      = new String[] { "Moon Name", "Planet", "Atmosphere",
                       "Population" };

   private String filter = "All";

   public int getColumnCount()
      return planetModelNames.length;

   public String getColumnName(int c) {
   return planetModelNames[c];



Core Swing Advanced Programming
By Kim Topley
ISBN: 0130832929
Publisher: Prentice Hall; December, 2000

Core Java 2, Volume II: Advanced Features (5th Edition)
By Cay Horstmann and Gary Cornell
Publisher: Prentice Hall; 5th edition (December 10, 2001)

A Swing Architecture Overview

Advanced MVC

Swing Model Filtering
By Mitch Goldstein

About the Author

Vlad Kofman is a System Architect working on projects under government defense contracts. He has also been involved with enterprise-level projects for major Wall Street firms and the U.S. government. His main interests are object-oriented programming methodologies and design patterns.

Page 2 of 2

This article was originally published on April 7, 2004

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date