- Forward


The Model-View-Controller Pattern
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Motivation
Back Forward
  • Many Objects:
    • Manage information/content
    • Present/display that information
    • React to events
  • Examples:
    • Components/widgets in a GUI
    • Database management systems
A Model of the M-V-C Pattern
Back Forward
images/mvc_pattern.gif
An Example: MVCTextField
Back Forward
images/MVCTextField_context.gif
An Example: MVCTextField (cont.)
Back Forward

Handling an arrowkeyPressed Event

images/MVCTextField_arrowkeyPressed.gif
An Example: MVCTextField (cont.)
Back Forward

Handling a mouseUp Event

images/MVCTextField_mouseUp.gif
An Example: MVCTextField (cont.)
Back Forward

Handling a characterKeyPressed Event

images/MVCTextField_characterkeyPressed.gif
An Example: An IntegerEntryField
Back Forward
  • Replace the Controller:
    • A '-' is OK but only in one locations
    • What about paste?
    Expand
  • Replace the Model:
    • Just need to determine if the String can be converted
    Expand
Benefits of the M-V-C Pattern
Back Forward

Creating an IntegerDocument

javaexamples/mvc/IntegerDocument.java
 
Benefits of the M-V-C Pattern (cont.)
Back Forward

Creating an IntegerEntryField

javaexamples/mvc/IntegerEntryField.java
 
Benefits of the M-V-C Pattern (cont.)
Back Forward
  • Problems with the IntegerDocument:
    • You can't include a leading '+'
    • You can't type just a leading '-'
  • Improvements to the IntegerDocument:
    • Have it "predict" future keystrokes but select them so that they will be replaced
Benefits of the M-V-C Pattern (cont.)
Back Forward

Skins/PLAF

javaexamples/lookandfeel/PLAFdemo.java
 
There's Always More to Learn
Back -