Use high-level MVC and POJOs with Swing

An introduction to the TikeSwing framework

1 2 Page 2
Page 2 of 2

A TikeSwing component can basically be any java.awt.Component. However, a component must implement suitable TikeSwing interfaces so that it can be integrated into the framework's MVC architecture. This should be quite a trivial task since it usually involves extending a standard Swing component with four simple methods. See the code below for an example. Integration with the model is implemented in the getModelValue() and setModelValue() methods. Notification of component value changes is implemented in the addViewListener() method. The method getYProperty() must be implemented for the framework's internal use.

The following code represents a simple text field that supports Integer objects:


public class YIntegerField extends JTextField implements YIModelComponent {

/** Gets value of this field for the model. */ public Object getModelValue() { try { return new Integer(getText()); } catch (Exception ex) { return null; } }

/** Sets the model value into this field. */ public void setModelValue(Object obj) { if (obj == null) { setText(""); } else { setText(obj.toString()); } }

/** Notifies the framework when the component value might have changed. */ public void addViewListener(final YController controller) { this.addFocusListener(new FocusAdapter() { public void focusLost(FocusEvent ev) { controller.updateModelAndController(YIntegerField.this); } }); }

// The rest is for the framework internal use, // the implementation must be copied to each new component: private YProperty myProperty = new YProperty();

public YProperty getYProperty() { return myProperty; } }

Other features

In addition to the MVC architecture, TikeSwing offers a wide set of other features that help Swing development. These features are not revolutionary and can be found in many already implemented Swing applications. But since it is unnecessary to reinvent the wheel in every application, some best practices of Swing development are included in the framework.

TikeSwing supports the creation of hierarchical structures for controllers, as described in the patterns HMVC and MVC++. The framework offers methods that create parent-child relationships between controllers, which leads to a consistent and clear class structure. These relationships also help communication within a client application and can be used together with well-known design patterns. TikeSwing supports the Chain of Responsibility pattern, where a request is passed until one of the objects (controllers) handles the event. TikeSwing also supports the Observer/Observable pattern: a controller class may send an event that can be handled by all the registered controllers.

TikeSwing also includes a mechanism for lazy data retrieval for tabbed panes. In a distributed system, getting data from a server for all the tabs at once might take a long time. To optimize the performance, it might prove necessary to get data for each tab just once, right after the tab is selected. The framework provides a mechanism that simplifies the implementation of this functionality, so the code complexity, especially in nested tabbed panes, is reduced.

Some applications check unsaved changes when the user triggers an event that could lose data in just edited fields. This event could be, for example, closing a window, changing a tabbed pane tab, or selecting a table row. TikeSwing provides tools that enable the checking in specific events. TikeSwing also automatically pops up an "Unsaved changes?" dialog box and delegates saving to a controller method. In addition, the framework remembers a view's state in a specific moment and can return the view to that state later. This means that the framework cancels changes without retrieving data from its original source (a server application).

Swing actions might prove useful when two or more components perform the same function. An Action object provides centralized event handling, but if the action is used in separate classes, the code gets easily more complex with increased coupling. TikeSwing includes a factory that centralizes the creation of actions, so an action can be used in separate view classes without direct coupling.

Swing components should be created, modified, and queried only from the event-dispatching thread, which complicates thread handling in a Swing application. The Swing Tutorial presents the SwingWorker class to provide some help for the issue. TikeSwing encapsulates SwingWorker and simplifies thread handling further. For example, some applications don't freeze during remote calls or I/O operations. With TikeSwing, a manageable, paint-able dialog can pop up during such operation with just a few lines of code.


TikeSwing simplifies Swing development with a high-level MVC architecture and POJO support. Using TikeSwing is reasonable, especially in a distributed environment, because POJOs returned by a server application can be used directly in a model class, which is automatically connected to a view class. The framework also includes several best practices that solve complex development issues. Therefore, TikeSwing reduces the code that must be written for a Swing client and speeds up development.

Swing itself offers a rich platform-independent user interface library. Swing development has been a part of all the important IDEs for years, so WYSIWYG layout editing, unit testing, and debugging are widely supported. Previous issues with workstation performance should not be a problem nowadays, and Java Web Start eases the distribution of Java applications. Compared to Web application frameworks, Swing provides tools to build more user-friendly interfaces without JavaScript support problems and reduces network traffic by executing client logic in a workstation.

Still, criticism against Swing complexity is justified. But with high-level MVC frameworks like TikeSwing, that complexity can be reduced, and Swing can be turned into a highly productive client technology. I hope that the Java community will adopt and develop a widely tried and tested open source MVC framework for Swing, which would make it a serious candidate among RIA technologies. Maybe projects like spring-richclient will come closer to this goal. While waiting, check out TikeSwing and evaluate how it would fit in your RIA project.

Tomi Tuomainen is a consultant and an architect for Entra e-Solutions and has worked with J2EE applications and Java client frameworks since 1999. He has a master's of science in computer science and he is a Sun Certified Enterprise Architect. His interests include (in addition to Java technologies) music, guitars, and gym training. You could say he is one of the strongest IT consultants in Finland.

Learn more about this topic

1 2 Page 2
Page 2 of 2