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

Page 4 of 6

Controller

The TikeSwing controller handles application flow by calling methods of the view and the model. A controller class must extend YController, which provides necessary methods for controlling the triangle. Usually, controllers also create view and model objects, but note that several views and controllers may share the same model object.

A controller class may catch user events in several ways. The TikeSwing components include reflection-based event handling: an event can be handled in a controller class by implementing a method with the appropriate signature. For example, a button with the MVC name myButton calls the method myButtonPressed() in a controller class (if implemented) when the user presses the button. This is often quite convenient compared to the standard Swing event listener interfaces and adapters.

On the other hand, typos in an event method signature are not revealed by the compiler, but so is the case with the Swing adapter classes: the compiler doesn't tell whether public void actionperformed is a new method or the overridden method. And since the listener interfaces often require many empty method implementations, the simplicity of reflection-based event handling should speed up the coding process. Alternatively, you could use the standard listeners in a view class and call a controller method manually.

The following code is an example controller for FindCustomerModel and FindCustomerView. The controller wires up the MVC structure by calling the setUpMVC() method and handles a reflection-based event triggered by findButton.

 public class FindCustomerController extends YController {
        
    private FindCustomerView view = new FindCustomerView();
    private FindCustomerModel model = new FindCustomerModel();

public FindCustomerController() { super(); setUpMVC(model, view); } public void findButtonPressed() { model.findCustomers(); } }


The YController is the heart of functionality in TikeSwing. In addition to the features described above, it provides many useful methods that can be used for:

  • Catching changes in a particular field
  • Sending and receiving messages between controllers
  • Keeping track of user changes
  • Canceling user changes
  • Catching exceptions thrown by a model class
  • Checking valid field values

TikeSwing components

TikeSwing is based on the idea that the component is responsible for handling the connected object in the model. This idea is reminiscent of the WholeNumberField demo in Sun's Swing Tutorial. The component must know how to present a model value on the screen and how to convert the user-given value back to the model.

The framework currently offers a wide set of components that should be enough for most applications to get started. The behavior of the framework components resembles that of the base Swing components, but of course you should read the Javadoc to understand the component interaction with the MVC classes (what kind of model fields the component can handle and what event methods it provides). TikeSwing components also offer additional features that simplify development. For example, a collection of POJOs can be used in YTable and YTree directly without creating any special component models.

  • Print
  • Feedback

Resources