ActionListener interface.
ActionListener interface requires that the class
define an actionPerformed(ActionEvent e) method.
addActionListener(...) method, passing
a parameter which is an object implementing ActionListener.
The class which defines the graphical user interface implements
ActionListener. The component uses this
for the action listener.
myButton.addActionListener(this);
Example p 234.
This doesn't scale -- what if there is more than one component that needs an action listener?
JButton b1 = new JButton("Hello");
b1.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
// do something for button b1
}
}
);
ActionListener myHelloListener = new HelloListener():
JButton b1 = new JButton("Hello");
b1.addActionListener(myHelloListener);
. . .
class HelloListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// do something for button b1
}
}
AbstractAction exitAction = new AbstractAction() {
public void actionPerformed( ActionEvent e ) {
if (gCurrentSkeema.isDirty()) {
int response = JOptionPane.showConfirmDialog(xc_window,
"Exit without saving changes?");
if (response == JOptionPane.YES_OPTION) {
System.exit(0);
}
} else {
System.exit(0); // Not dirty, can quit.
}
}
};