Java Reference
In-Depth Information
Note
When the
JPopupMenu
is displayed in either a
JPanel
or a
Panel
, the outermost window relies on
the layering effect of the
JRootPane
to ensure that the pop-up panel is displayed at the appropriate position
in front of the other components. Chapter 8 describes the
JRootPane
class in more detail.
■
If you need to enable the display of a
Panel
, you can configure it at the individual
JPopupMenu
level or for your entire applet or application. At the individual pop-up level, just set the
lightWeightPopupEnabled
property to
false
. At the system level, this is done as follows:
// From now on, all JPopupMenus will be heavyweight
JPopupMenu.setDefaultLightWeightPopupEnabled(false);
The method must be called before creating the pop-up menu.
JPopupMenu
objects created
before the change will have the original value (the default is
true
).
Watching for Pop-Up Menu Visibility
Like the
JMenu
, the
JPopupMenu
has a special event/listener combination to watch for when the
pop-up menu is about to become visible, invisible, or canceled. The event is
PopupMenuEvent
,
and the listener is
PopupMenuListener
. The event class simply references the source pop-up
menu of the event.
public class PopupMenuEvent extends EventObject {
public PopupMenuEvent(Object source);
}
When a
JPopupMenu
fires the event, any registered
PopupMenuListener
objects are notified
through one of its three interface methods. This lets you customize the current menu items
based on the system state or who/what the pop-up menu invoker happens to be. The
PopupMenuListener
interface definition follows:
public interface PopupMenuListener extends EventListener {
public void popupMenuCanceled(PopupMenuEvent e);
public void popupMenuWillBecomeInvisible(PopupMenuEvent e);
public void popupMenuWillBecomeVisible(PopupMenuEvent e);
}
Customizing a JPopupMenu Look and Feel
Each installable Swing look and feel provides a different
JPopupMenu
appearance and set of
default
UIResource
values. Figure 6-6 shows the appearance of the
JPopupMenu
component for
the preinstalled set of look and feel types: Motif, Windows, and Ocean. Notice that of the
predefined look and feel classes, only Motif uses the
title
property of the
JPopupMenu
.