Java Reference
In-Depth Information
directly handling mouse and keyboard events that occur in it. The AppletMenuBar
class, listed in Example 10-29, is a custom component of this type. As its name
implies, this component can display a menubar in an applet. Most web browsers
(at least at the time of this writing) do not have the Swing components prein-
stalled. So, for compatibility with these browsers, many applets are written using
only AWT components. One of the shortcomings of the AWT is that only Frame
objects can have menubars; the Applet class cannot display a java.awt.Menubar .
Therefore, the AppletMenuBar component is a custom AWT component that simu-
lates a menubar. It provides a paint() method that draws the menubar, primarily
the menu labels, and defines low-level event handling methods that track the
mouse, perform “rollover” effects, and popup menus (using the java.awt.Popup-
Menu class) when appropriate. The AppletMenuBar class also includes an inner
class named Demo that implements a demonstration applet.
In order to understand the AppletMenuBar component, it may help to see how the
component might be used in an applet. The following lines show a simple init()
method for an applet that uses the class:
public void init() {
AppletMenuBar menubar = new AppletMenuBar();
// Create the menubar
menubar.setForeground(Color.black);
// Set properties on it
menubar.setHighlightColor(Color.red);
menubar.setFont(new Font("helvetica", Font.BOLD, 12));
this.setLayout(new BorderLayout());
this.add(menubar, BorderLayout.NORTH);
// Add it at the applet top
// Create a couple of popup menus and add dummy items to them
PopupMenu file = new PopupMenu();
file.add("New..."); file.add("Open..."); file.add("Save As...");
PopupMenu edit = new PopupMenu();
edit.add("Cut"); edit.add("Copy"); edit.add("Paste");
// Add the popup menus (with labels) to the menubar
menubar.addMenu("File", file);
menubar.addMenu("Edit", edit);
}
When you study AppletMenuBar , there are a number of things to notice. The
paint() method is the heart of the component, as it does all of the drawing. The
processMouseEvent() method, which handles all the mouse events, is equally
important. Both methods override standard methods inherited from the Component
class, and both rely on the measure() method, which precomputes the size and
position of each of the menu labels. AppletMenuBar defines various property
accessor methods, and many of the property setter methods respond to property
changes by causing the menubar to redraw or remeasure itself. The other overrid-
den methods, getPreferedSize() , getMinimumSize() , and isFocusTraversable() ,
all provide important information about the component that is used by the compo-
nent's container. Finally, notice that AppletMenuBar does not fire any events or
define event listener registration methods. The MenuItem objects in the popup
menus do all the event handling, so there is simply no need for them here. Still,
generating events is a common task for custom components, as we saw with
ItemChooser .
Search WWH ::




Custom Search