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 2 of 6

Model

The TikeSwing model is a JavaBeans component that contains data for the view. A model class may include nested JavaBeans, arrays, maps, and collections. All model fields must have appropriate get and set methods as described in the JavaBeans standard. In this sense, TikeSwing works like many Web application frameworks, so it should be easy to reuse model classes between different technologies.

The YModel is the base class for models. It provides methods for reporting data changes. When a notification is triggered, the framework updates the changes to the connected views. In a distributed environment, a model class has methods that get POJOs from a server application (or usually from a business delegate that hides a business service's implementation details). The POJOs are stored in the model itself, which is responsible for notifying observers. In some MVC architectures, a controller class communicates with a server, and POJOs are stored in the controller. However, the TikeSwing approach, with a separate YModel class, has benefits: the controller concentrates purely on the flow, and additional methods (that operate on model data) can be added on the client side. YModel also follows the traditional MVC pattern, so the responsibilities of the MVC classes are cleanly divided.

The following code represents a model class that finds customers with given parameters. The model fields name and id are used for search criteria, and customers is a collection of Customer POJOs containing the search results. The method findCustomers() gets customers from a server application via customerServiceDelegate. The framework automatically updates the connected views, when the method notifyObservers() is called.

 public class FindCustomerModel extends YModel {
    
   private String name;
   private String id;
    
   private Collection customers;

private CustomerServiceDelegate delegate = new CustomerServiceDelegate(); public void findCustomers() { setCustomers(delegate.findCustomers(id, name)); notifyObservers("customers"); }

public void setCustomers(Collection customers) { this.customers = customers; }

public Collection getCustomers() { return customers; } public void setId(String id) { this.id = id; }

public String getId() { return id; }

public void setName(String name) { this.name = name; } public String getName() { return name; } }


View

The TikeSwing view is a Swing component containing other Swing components. Usually a view class is a panel, a dialog, or a frame, which creates child components and adds them to itself (just like in traditional Swing development). However, all the components used in a TikeSwing application must implement appropriate interfaces to connect to the framework's MVC architecture. Fortunately, the framework includes a wide set of already implemented components for this purpose.

A special name must be set to a view component so that the framework can copy data between the component and the named model field. The naming convention is similar to those used in Web application frameworks and the Apache BeanUtils library (which is actually used in the framework implementation). The following naming formats are supported:

  • Print
  • Feedback

Resources