- Forward


GUI Layout Using the M-V-C Pattern
A Discussion with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Motivation
Back SMYC Forward
  • The Issue:
    • The classes that constitute a product's GUI (not the individual components/containers) are rarely cohesive
  • The Question:
    • Can the model-view-controller pattern be used to improve GUI construction
Using the M-V-C Pattern
Back SMYC Forward
  • The Model:
    • The set of "simple" components that comprise the "complicated" component (i.e., the container)
  • The View:
    • The object that is responsible for laying-out the "complicated" container
  • The Controller:
    • Can be ignored since the "simple" components all have their own controller
An Abstract Design
Back SMYC Forward
  • The Model:
    • A JContainerModel interface
  • The View:
    • A JContainerView interface
Possible Ways to Access Components in the Model
Back SMYC Forward
  • Give all of the "simple" components in the model package visibility
    • But we may want to organize the views into packages
  • Have accessors for all "simple" components
    • But a "complicated" component could make use of a very large number of "simple" components
  • Use name-value pairs
The Model Interfaces
Back SMYC Forward
javaexamples/gui/mvc/JContainerModel.java
 
The View Interfaces
Back SMYC Forward
javaexamples/gui/mvc/JContainerView.java
 
Different Ways to Construct the Model and View
Back SMYC Forward
  • Have the model construct the components and then construct the view
  • Include constructComponents() and areComponentsConstructed() methods in the model and let either the model or the view construct the components
  • Have a "factory" that constructs both the model and the view
An Example: The Model
Back SMYC Forward
javaexamples/gui/mvc/JListEditor.java
 
An Example (cont.): An Abstract View
Back SMYC Forward
javaexamples/gui/mvc/JListEditorView.java
 
An Example (cont.): One Concrete View
Back SMYC Forward
javaexamples/gui/mvc/JListEditorTraditionalView.java
 
An Example (cont.): Another Concrete View
Back SMYC Forward
javaexamples/gui/mvc/JListEditorModernView.java
 
Adding Flexibility
Back SMYC Forward
  • Use generic subjects/producers in place of components in the model
  • For example, rather than have the model contain a JButton you can have it contain an object that implements the ActionSubject interface (i.e. the subject of a ActionListener )
There's Always More to Learn
Back -