Swing menu and toolbar (2)

Source: Internet
Author: User

6.1.4 JMenuItem class

The JMenuItem component is a predefined component that you can select on the menu bar. As a subclass of AbstractButton, JMenuItem is a special button component and its behavior is similar to JButton. In addition to being a subclass of AbstractButton, The JMenuItem class shares the data model of JButton (implemented by the ButtonModel interface and DefaultButtonModel ).

Create a JMenuItem component

JMenuItem has six constructors. These constructors enable us to initialize the string or icon of the menu item and the hotkey of the menu item. There is no explicit constructor that allows us to set all three options during creation, unless we use them as part of the Action.

public JMenuItem()JMenuItem jMenuItem = new JMenuItem(); public JMenuItem(Icon icon)Icon atIcon = new ImageIcon("at.gif");JMenuItem jMenuItem = new JMenuItem(atIcon); public JMenuItem(String text)JMenuItem jMenuItem = new JMenuItem("Cut"); public JMenuItem(String text, Icon icon)Icon atIcon = new ImageIcon("at.gif");JMenuItem jMenuItem = new JMenuItem("Options", atIcon); public JMenuItem(String text, int mnemonic)JMenuItem jMenuItem = new JMenuItem("Cut", KeyEvent.VK_T); public JMenuItem(Action action)Action action = ...;JMenuItem jMenuItem = new JMenuItem(action);

The hot key allows us to browse and select the menu through the keyboard. For example, if the menu item appears in the open Edit menu on Windows, you can simply press Alt-T to select the Cut menu. The shortcut key of a menu item usually appears as an underline in the menu text label. However, if the character does not appear in the text tag, or there is no text tag, the user will not receive any obvious prompt. Characters are identified by different constants in the java. awt. event. KeyEvent class.

Other platforms may provide other selected hotkeys. Generally, the Alt key is used on Unix platforms, while the Command key is used on Macintosh platforms.

JMenuItem attributes

JMenuItem has multiple attributes. About 100 attributes are inherited by various superclasses. Table 6-3 shows the specific 10 attributes of JMenuItem.

JMenuItem attributes

Attribute name
Data Type



Read/write binding








Write-only binding

MenuDragMouseListener []


MenuKeyListener []


MenuElement []



Write-only binding



An interesting property is accelerator. As explained in chapter 2nd, KeyStroke is a factory class that allows us to create instances based on combinations of buttons and identifiers. For example, the following code statement is from the example in list 6-1 in this chapter. use Ctrl-X as the shortcut key to associate it with a specific menu item:

KeyStroke ctrlXKeyStroke = KeyStroke.getKeyStroke("control X");cutMenuItem.setAccelerator(ctrlXKeyStroke);

The read-only component and subElement attributes are part of the MenuElement interface implemented by JMenuItem. The component Attribute is the Renderer of the menu item (JMenuItem itself ). The subElement attribute is empty (that is, an empty array, rather than null), because JMenuItem does not have a subclass.

Process JMenuItem events

We can use at least five different methods within JMenuItem to process events. The component inherits the capabilities that allow us to trigger ChangeEvent and ActionEvent through the methods registered by mongoactbutton's ChangeListener and ActionListener. The JMenuItem component supports registering the MenuKeyListener and MenuDragMouseListener objects when the MenuKeyEvent and MenuDragMouseEvent events occur. These technologies will be discussed in later chapters. The fifth method is to pass the Action to the JMenuItem constructor, which is similar to a special method for listening with ActionListener. For more information about using Action, see the discussion about using Action objects in the menu in the "JMenu class" section later in this chapter.

Use ChangeListener to listen to JMenuItem events

Generally, we do not register ChangeListener with JMenuItem. However, presenting an ideal example helps to explain more clearly the changes of JMenuItem's ButtonModel data model. The event changes are the same arm, press, and select as JButton. However, their names are somewhat confusing because the selected model attributes are not set.

When you move the cursor over the menu option and the menu becomes selected, JMenuItem is armed. When the user releases the mouse button, JMenuItem is pressed. After pressing, the menu item changes to unpressed and unarmed. When the menu item changes to "pressed" or "not pressed", AbstractButton will be notified of model changes, so that the ActionListener object registered in the menu item will be notified. The button model of a common JMenuItem is not reported to be selected. If we move the mouse over another menu item without selection, the first menu item will automatically change unarmed. To help us better understand different changes, figure 6-5 shows a sequence diagram.

Use ActionListener to listen to JMenuItem events

The better listener associated with JMenuItem is ActionListener, or an Action is passed to the constructor. This allows us to determine which menu item is selected. When you release the mouse button on the JMenuItem that is part of the Open menu, the registered ActionListener object will be notified. If you select a menu by pressing the keyboard (arrow key or hot key) or by pressing the shortcut key, the registered listener will also be notified.

An action occurs when you want the menu to be selected. You must add an ActionListener for each JMenuItem. There is no automatic method so that we can register an ActionListener for JMenu or JMenuBar so that the JMenuItem object contained in it can notify an ActionListener.

The example program in list 6-1 associates an identical ActionListener for each JMenuItem:

class MenuActionListener implements ActionListener {  public void actionPerformed(ActionEvent e) {    System.out.println("Selected: " + e.getActionCommand());  }}

More often, however, we associate a different action for each menu item so that each menu item can respond differently.

Tip: we do not need to create a custom ActionListener for the component and register it. We can create a custom Action and call the setAction () method on the component.

Use MenuKeyListener to listen to JMenuItem events

MenuKeyEvent is

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.