Java Reference
In-Depth Information
Table 6-3. JMenuItem Properties
Property Name
Data Type
Access
accelerator
KeyStroke
Read-write bound
accessibleContext
AccessibleContext
Read-only
armed
boolean
Read-write
component
Component
Read-only
enabled
boolean
Write-only bound
menuDragMouseListeners
MenuDragMouseListener[ ]
Read-only
menuKeyListeners
MenuKeyListener[ ]
Read-only
subElements
MenuElement[ ]
Read-only
UI
MenuElementUI
Write-only bound
UIClassID
String
Read-only
One truly interesting property is accelerator . As explained in Chapter 2, KeyStroke is a
factory class that lets you create instances based on key and modifier combinations. For instance,
the following statements, from the example in Listing 6-1 earlier in this chapter, associate Ctrl-X as
the accelerator for one particular menu item:
KeyStroke ctrlXKeyStroke=KeyStroke.getKeyStroke("control X");
cutMenuItem.setAccelerator(ctrlXKeyStroke);
The read-only component and subElements properties are part of the MenuElement interface,
which JMenuItem implements. The component property is the menu item renderer (the JMenuItem
itself). The subElements property is empty (that is, an empty array, not null ), because a JMenuItem
has no children.
Note Swing menus don't use AWT's MenuShortcut class.
Handling JMenuItem Events
You can handle events within a JMenuItem in at least five different ways. The component inherits
the ability to allow you to listen for the firing of ChangeEvent and ActionEvent through the
ChangeListener and ActionListener registration methods of AbstractButton . In addition, the
JMenuItem component supports registering MenuKeyListener and MenuDragMouseListener
objects when MenuKeyEvent and MenuDragMouseEvent events happen. These techniques are
discussed in the following sections. A fifth way is to pass an Action to the JMenuItem constructor,
which is like a specialized way of listening with an ActionListener . For more on using Action ,
see the discussion of using Action objects with menus, in the “JMenu Class” section a little later
in this chapter.
 
Search WWH ::




Custom Search