Java Reference
In-Depth Information
Listening to JMenuItem Events with a ChangeListener
Normally, you wouldn't register a ChangeListener with a JMenuItem . However, demonstrating
one hypothetical case helps to clarify the data model changes of the JMenuItem with respect to
its ButtonModel . The changes with regard to arming, pressing, and selecting are the same as
with a JButton . However, their naming might be a little confusing because the selected property of
the model is never set.
A JMenuItem is armed when the mouse passes over the menu choice and it becomes selected.
A JMenuItem is pressed when the user releases the mouse button over it. Immediately after being
pressed, the menu item becomes unpressed and unarmed. Between the menu item being
pressed and unpressed, the AbstractButton is notified of the model changes, causing any regis-
tered ActionListener objects of the menu item to be notified. The button model for a plain
JMenuItem never reports being selected. If you move the mouse to another menu item without
selecting, the first menu item automatically becomes unarmed. To help you better visualize
the different changes, Figure 6-5 shows a sequence diagram.
ActionListener
List of
JMenuItem
ChangeListener
List of
JMenuItem
ActionListener
List of
ButtonModel
JMenuItem
ButtonModel
Registers with
Registers with
Mouse Enters/Menu Selection Changed
Armed
Mouse Released
Pressed
Notifies
Pressed
Notifies
No Longer Pressed
No Longer Armed
Figure 6-5. JMenuItem selection sequence diagram
Note Subclasses of JMenuItem can have their button model selected property set, like a radio button—
but the predefined JMenuItem cannot.
 
Search WWH ::




Custom Search