MVC meets Swing
Explore the underpinnings of the JFC's Swing components
By Todd Sundsted, JavaWorld.com, 04/01/98
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
A good software user interface often finds its genesis in the user interfaces present in the physical world. Consider for
a moment a simple button like one of the keys on the keyboard in front of you. With such a button there is a clean separation
between the parts that compose the button's mechanism and the parts that compose its façade. The building block called a keyboard
key is actually composed of two pieces. Once piece gives it its button-like behavior. The other piece is responsible for its
appearance.
This construction turns out to be a powerful design feature. It encourages reuse rather than redesign. Because your keyboard's
keys were designed that way, it's possible to reuse the button mechanism design, and replace the key tops to create a new
key rather than designing each key from scratch. This creates a substantial savings in design effort and time.
Not surprisingly, similar benefits occur when this technique is applied to software development. One commonly used implementation
of this technique in software is the design pattern called Model/View/Controller (MVC).
That's all well and good, but you're probably wondering how this relates to the Swing user interface components in the Java
Foundation Classes (JFC). Well, I'll tell you.
While the MVC design pattern is typically used for constructing entire user interfaces, the designers of the JFC used it as
the basis for each individual Swing user interface component. Each user interface component (whether a table, button, or scrollbar)
has a model, a view, and a controller. Furthermore, the model, view, and controller pieces can change, even while the component
is in use. The result is a user interface toolkit of almost unmatched flexibility.
Let me show you how it works.
The MVC design pattern
If you are not familiar with the MVC design pattern, I recommend you review "Observer and Observable", one of my earlier articles that discusses this topic in much greater detail and provides the groundwork for this month's
column.
As I mentioned a moment ago, the MVC design pattern separates a software component into three distinct pieces: a model, a
view, and a controller.

Figure 1. MVC design pattern
The model is the piece that represents the state and low-level behavior of the component. It manages the state and conducts all transformations
on that state. The model has no specific knowledge of either its controllers or its views. The system itself maintains links
between model and views and notifies the views when the model changes state.
The view is the piece that manages the visual display of the state represented by the model. A model can have more than one view,
but that is typically not the case in the Swing set.
The controller is the piece that manages user interaction with the model. It provides the mechanism by which changes are made to the state
of the model.
Using the keyboard key example, the model corresponds to the key's mechanism, and the view and controller correspond to the
key's façade.
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Resources
- The Swing Tutorial http://developer.javasoft.com/developer/onlineTraining/swing/
- The JavaSoft Swing API http://www.javasoft.com/products/jfc/swingdoc-current/api/packages.html
- The Swing Connection http://www.javasoft.com/products/jfc/swingdoc-current/
- Download this article and the complete source as a gzipped tar file http://www.javaworld.com/javaworld/jw-04-1998/howto/jw-04-howto.tar.gz
- Download this article and the complete source as a zip file http://www.javaworld.com/javaworld/jw-04-1998/howto/jw-04-howto.zip
- Previous How-To Java articles
- "Put your user interface on a diet" Replace those heavyweight components with leaner, meaner lightweight components.
- "Internationalize dynamic messages" Build flexible formatters for international applications with Java 1.1.
- "Localize this!" Use resource bundles to make your applications multicultural.
- "Write world-class applications" From Chicago to Copenhagen, Colombia to Cameroon -- Java provides the tools for writing truly international applications.
- "Use the two "R"s of Java 1.1 -- Readers and Writers" Learn how to use the two new additions to the
java.io package -- class Reader and class Writer -- to filter out unwanted e-mail.
- "Waging war on electronic junk mail" Put Java on the front line in the war against electronic junk mail.
- "Build dynamically extensible applications" Find out how to build programs that you can extend dynamically -- even while they execute.
- "3D computer graphicsGetting the hang of VRML" Learn about VRML and how you can use it to define your own virtual world.
- "3D computer graphicsMoving from wire-frame drawings to solid, shaded models" Find out how to create surfaces and add illumination to make your models more realistic.
- "3D computer graphicsSlide it, spin it, make it move -- transforming your virtual world" Learn how to make your virtual world satisfy even the toughest customer.
- "3D computer graphicsModel your world" From its start as an exotic research topic in government and university labs, virtual reality is making its move into the
mainstream of corporate America -- find out how you can gain entry into this elite club.
- "When static images just don't make the cut" Learn how to spice up your applets and applications with animated images.
- "How Java uses the the producer/consumer model to handle images -- An insider's look" Learn more about Java's powerful image-handling technique, then follow my simple procedures for building your own producer
and consumer components.
- "Learn how applets load network-based images asynchronously" Here's a close look at the way Java applets handle images in a network-based environment.
- "Drawing text is easy with three Java classes" Find out how to create text that's visually appealing through this explanation of what classes to use and how they work
together.
- "Examining HotSpot, an object-oriented drawing program" Learn how the pieces of the Java language and class library fit together through a study of this Java program
- "Using the Graphics class" A close look at the Graphics class and the drawing primitives it provides, and a demonstration of its use.
- "Observer and Observable" An introduction to the Observer interface and Observable class using the Model/View/Controller architecture as a guide.
- "The effective user interface" Five ways to enhance the appearance and effectiveness of your user interface
- "Java and event handling" How events are passed to user interface components, how to create event handlers, and more.
- "Introduction to the AWT" A description of Java's user interface toolkit.
MVC unleashed in SWINGBy Anonymous on May 20, 2009, 5:42 amA good article on MVC usage in SWING.
Reply | Read entire comment
View all comments