Professional Documents
Culture Documents
Java Events and The MVC Architecture
Java Events and The MVC Architecture
There was a major change in Java 1.0 and 1.1 regarding event
handling. To understand the use of events with their corresponding
listeners there are a number of sources. Java in a Nutshell, 2nd edition
addresses the issue on pages 150- 157. Core Java, Volume 1 has Event
Handling in Chapter 8 which uses this Delegation Event Model.
It is a good design tool to be familiar with the MVC pattern
which is the abstraction of this methodology. When using advanced Java tools one can
use this idea extensively.
Following is some sample code that uses the model: This is taken from
the Java AWT: Delegation Event Model page at java.sun.com
The main focus is to see
import java.awt.*;
import java.awt.event.*;
Button b;
f.add(b = new Button("Search"));
b.addActionListener(searchCmd);
f.add(b = new Button("Sort"));
b.addActionListener(sortCmd);
List l;
f.add(l = new List());
l.add("Alphabetical");
l.add("Chronological");
l.addActionListener(sortCmd);
f.pack();
f.show();
}
}