Java Reference
In-Depth Information
Method
Description
void addPropertyChangeListener
(PropertyChangeListener
listener)
This adds the listener passed as an argument that
listens for changes to properties such as the
enabled state of the object. This is used by a
container for an action object to track property
changes.
void
removePropertyChangeListener(
PropertyChangeListener
listener)
This removes the listener passed as an argument.
This is also for use by a Container object.
Of course, since the Action interface extends the
ActionListener interface, it also incorporates
the ActionPerformed() method that you are
already familiar with.
So far, all we seem to have with this interface is a license to do a lot of work in implementing it but it's
not as bad as that. The javax.swing package defines a class, AbstractAction , that already
implements the Action interface. If you extend this class to create your own action class, you get a
basic infrastructure for free. Let's try it out in the context of Sketcher.
Using Actions as Menu Items
This will involve major surgery on our SketchFrame class. Although we'll be throwing away all those
fancy varieties of menu items we spent so much time putting together, at least you know how they work
now, and we'll end up with much less code after re-engineering the class, as you'll see. As the saying
goes, you've got to crack a few eggs to make a soufflé.
We'll go back nearly to square one and reconstruct the class definition. First we will delete a lot of code
from the existing class definition. Comments show where we will add code to re-implement the menus
using actions. Get your definition of SketchFrame to the following state:
// Frame for the Sketcher application
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SketchFrame extends JFrame implements Constants {
// Constructor
public SketchFrame(String title) {
setTitle(title); // Set the window title
setJMenuBar(menuBar); // Add the menu bar to the window
setDefaultCloseOperation(EXIT _ ON _ CLOSE); // Default is exit the application
JMenu fileMenu = new JMenu("File"); // Create File menu
JMenu elementMenu = new JMenu("Elements"); // Create Elements menu
fileMenu.setMnemonic('F'); // Create shortcut
elementMenu.setMnemonic('E'); // Create shortcut
// We will construct the file pull down menu here using actions...
Search WWH ::




Custom Search