Java Reference
In-Depth Information
the window's content pane to cyan, and line 150 attaches the JLabel to the CENTER of the
content pane's BorderLayout .
ItemHandler method actionPerformed (lines 157-181) uses two for statements to
determine which font or color menu item generated the event and sets the font or color of
the JLabel displayLabel , respectively. The if condition at line 163 uses Abstract-
Button method isSelected to determine the selected JRadioButtonMenuItem . The if
condition at line 173 invokes the event object's getSource method to get a reference to
the JRadioButtonMenuItem that generated the event. Line 176 invokes AbstractButton
method getText to obtain the name of the font from the menu item.
StyleHandler method itemStateChanged (lines 188-207) is called if the user selects
a JCheckBoxMenuItem in the fontMenu . Lines 195-203 determine which JCheckBoxMenu-
Item s are selected and use their combined state to determine the new font style.
22.5 JPopupMenu
Applications often provide context-sensitive pop-up menus for several reasons—they can
be convenient, there might not be a menu bar and the options they display can be specific
to individual on-screen components. In Swing, such menus are created with class JPopup-
Menu (a subclass of JComponent ). These menus provide options that are specific to the
component for which the popup trigger event occurred—on most systems, when the user
presses and releases the right mouse button.
Look-and-Feel Observation 22.8
The pop-up trigger event is platform specific. On most platforms that use a mouse with
multiple buttons, the pop-up trigger event occurs when the user clicks the right mouse but-
ton on a component that supports a pop-up menu.
The application in Figs. 22.7-22.8 creates a JPopupMenu that allows the user to select
one of three colors and change the background color of the window. When the user clicks
the right mouse button on the PopupFrame window's background, a JPopupMenu con-
taining colors appears. If the user clicks a JRadioButtonMenuItem for a color, ItemHandler
method actionPerformed changes the background color of the window's content pane.
Line 25 of the PopupFrame constructor (Fig. 22.7, lines 21-70) creates an instance of
class ItemHandler (declared in lines 73-89) that will process the item events from the
menu items in the pop-up menu. Line 29 creates the JPopupMenu . The for statement
(lines 33-39) creates a JRadioButtonMenuItem object (line 35), adds it to popupMenu (line
36), adds it to ButtonGroup colorGroup (line 37) to maintain one selected JRadioBut-
tonMenuItem at a time and registers its ActionListener (line 38). Line 41 sets the initial
background to white by invoking method setBackground .
1
// Fig. 22.7: PopupFrame.java
2
// Demonstrating JPopupMenus.
3
import java.awt.Color;
4
import java.awt.event.MouseAdapter;
5
import java.awt.event.MouseEvent;
6
import java.awt.event.ActionListener;
Fig. 22.7 | JPopupMenu for selecting colors. (Part 1 of 3.)
 
 
Search WWH ::




Custom Search