Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Use high-level MVC and POJOs with Swing

An introduction to the TikeSwing framework

  • Print
  • Feedback

The rise of rich Internet applications (RIAs) has lately become a hot topic in the Java community. In addition to new technologies like AJAX (Asynchronous JavaScript and XML), MacroMedia Flex, and Laszlo, the good old Swing with Java Web Start has also been proposed as an RIA technology.

However, many in the Java community criticize Java Foundation Classes (JFC) and Swing. Swing doesn't offer much help in creating a high-level Model-View-Controller (MVC) client architecture. This proves frustrating in the J2EE world, where any reasonable server application returns transfer objects, or so-called plain old Java objects (POJOs), to a client. A lot of manual coding is required to map POJO fields to Swing components and vice versa.

Also, implementing other features like thread handling and field validation with Swing often proves quite laborious. And sometimes Swing components are just hard to use: creating a suitable table or a tree model usually requires a lot of code and investigation into the Swing Javadoc API.

TikeSwing is an open source Swing framework that provides a high-level MVC architecture and automates communication between models, views, and controllers. The framework simplifies the usage of Swing components and supports the POJO programming model by connecting view components directly to JavaBeans.

This article demonstrates how to build a clear MVC architecture with TikeSwing. It also presents principles for creating TikeSwing components and briefly describes best practices and mechanisms included in the framework.

MVC architecture

The well-known MVC paradigm is generally recommended as the fundamental architecture for GUI development. Many variations of the pattern are available, like MVC++, HMVC (Hierarchical MVC), MVC Model 2, MVC Push, and MVC Pull, with each emphasizing slightly different issues. TikeSwing is based on the following MVC principles:

  • Model:
    • An abstraction of some real-world process or system
    • Encapsulates data and functions that operate on it
    • Notifies observers when data changes
  • View:
    • The system's user interface
    • Attaches to the model and renders its contents to the display surface
    • Automatically redraws the affected part when the model changes
  • Controller:
    • Controls the application flow
    • Accepts input from the user and instructs the model and the view to perform actions based on that input

The following diagram represents the MVC class structure in TikeSwing.

Figure 1. MVC class diagram of an application using TikeSwing

The MyModel, MyView, and MyController classes are implemented by an application using the framework. MyModel and MyController extend the TikeSwing classes YModel and YController. A view class can be any java.awt.Component that implements the YIComponent interface.

TikeSwing does not use any configuration files for setting up the class structure. Extending the appropriate classes is enough since YController, YModel, and view components provide the required functionality. The following sections describe how to implement the model, view, and controller classes with TikeSwing.

  • Print
  • Feedback

Resources