A variation of the model-view-controller architecture combines the controller with the view. In this case, a view not only presents the data, but is also used as an interface to interact with the user and accept user input, as shown in Figure 30.5.
For example, you can modify the view in the preceding example to enable the user to change the circle's radius using the mouse. When the left mouse button is clicked, the radius is increased by 5 pixels. When the right mouse button is clicked, the radius is decreased by 5 pixels. The new view, named ViewController , can be implemented by extending CircleView , as follows :
public class ViewController extends CircleView { public ViewController() { // Register mouse listener addMouseListener( new java.awt.event.MouseAdapter() { public void mousePressed(java.awt.event.MouseEvent e) { CircleModel model = getModel(); // Get model
if (model == null ) return ; if (e.isMetaDown()) model.setRadius(model.getRadius() - 5 ); // Right button else model.setRadius(model.getRadius() + 5 ); // Left button } }); } }
Another variation of the model-view-controller architecture adds some of the data from the model to the view so that frequently used data can be accessed directly from the view. Swing components are designed using the MVC architecture. Each Swing GUI component is a view that uses a model to store data. A Swing GUI component contains some data in the model so that it can be accessed directly from the component.