Events
The 1.02 model
- All events are handled by
handleEvent()
- Events are performed by overriding events like
mouseDown(), keyDown(),
etc.
- Problems:
- No way to know which events generated by which components
- Different components use different fields of the
event object

The 1.1 Model
- Components "register" with objects which listen for changes
- Listener objects are responsible for handling the event
- Basic ideas:
- Every event has a source (
getSource() )
- Every AWT event has type value (
getID() )
- The event source maintains a list of event listeners that are interested in
being notified
- Adapters are overridable methods for events (MouseListener)

Actions
- Give programmers the ability to package commonly used events
- The actions can be shared among containers like JMenus, JPopupMenus, JToolbars, etc.
- Disabling an action disables it everywhere through PropertyChangeEvents

Here are our actions:
// NewAction.java
import java.awt.event.ActionEvent;
import javax.swing.*;
public class NewAction extends AbstractAction {
MiniEdit theApp;
public NewAction(MiniEdit theApp) {
super("New", new ImageIcon("images/new.gif"));
this.theApp = theApp;
}
public void actionPerformed(ActionEvent event) {
theApp.addToDesktop(new EditFrame());
}
}
|
// OpenAction.java
import java.awt.event.ActionEvent;
import javax.swing.*;
public class OpenAction extends AbstractAction {
MiniEdit theApp;
public OpenAction(MiniEdit theApp) {
super("Open", new ImageIcon("images/open.gif"));
this.theApp = theApp;
}
public void actionPerformed(ActionEvent event) {
JFileChooser chooser = new JFileChooser();
int option = chooser.showOpenDialog(theApp);
if (option == JFileChooser.APPROVE_OPTION) {
System.out.println("You opened " +
chooser.getSelectedFile().getName());
} else {
System.out.println("You canceled.");
}
}
}
|
//QuitAction.java
import java.awt.event.ActionEvent;
import javax.swing.*;
public class QuitAction extends AbstractAction {
MiniEdit theApp;
public QuitAction(MiniEdit theApp) {
super("Quit");
this.theApp = theApp;
}
public void actionPerformed(ActionEvent event) {
theApp.quit();
}
}
|
|