Java Reference
In-Depth Information
Listening to JMenu Events with a ChangeListener
As with a JMenuItem , you can register a ChangeListener with a JMenu if you're interested in making
changes to the underlying ButtonModel . Surprisingly, the only possible state change to the
ButtonModel with a JMenu is with the selected property. When selected, the JMenu displays its
menu items. When not selected, the pop-up goes away.
Listening to JMenu Events with a MenuListener
The better way to listen for when a pop-up is displayed or hidden is by registering MenuListener
objects with your JMenu objects. Its definition follows:
public interface MenuListener extends EventListener {
public void menuCanceled(MenuEvent e);
public void menuDeselected(MenuEvent e);
public void menuSelected(MenuEvent e);
}
With a registered MenuListener , you're notified when a JMenu is selected before the pop-up
menu is opened with the menu's choices. This allows you to customize its menu choices on the
fly at runtime, with some potential interaction performance penalties. Besides being told when
the associated pop-up menu is to be posted, you're also notified when the menu has been
deselected and when the menu has been canceled. As the following MenuEvent class definition
shows, the only piece of information that comes with the event is the source (the menu):
public class MenuEvent extends EventObject {
public MenuEvent(Object source);
}
Tip If you choose to customize the items on a JMenu dynamically, be sure to call revalidate() ,
because the component waits until you are done before updating the display.
Customizing a JMenu Look and Feel
As with the JMenuBar and JMenuItem , the predefined look and feel classes provide a different
JMenu appearance and set of default UIResource values. Figure 6-3 shows the appearance of the
JMenu object for the preinstalled set of look and feel types.
The available set of UIResource -related properties for a JMenu is shown in Table 6-6. For the
JMenu component, there are 30 different properties.
 
Search WWH ::




Custom Search