Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 4 years, 2 months ago. Active 4 years, 2 months ago. Viewed 5k times. Improve this question. Squilly Squilly 27 1 1 silver badge 6 6 bronze badges. Maybe take a closer look at How to use menus — MadProgrammer. For better help sooner, post a minimal reproducible example or Short, Self Contained, Correct Example.
Add a comment. Active Oldest Votes. Improve this answer. The following figure shows many menu-related components: a menu bar, menus, menu items, radio button menu items, check box menu items, and separators.
As you can see, a menu item can have either an image or text, or both. You can also specify other properties, such as font and color. The rest of this section teaches you about the menu components and tells you how to use various menu features:. As the figure shows, menu items including menus are simply buttons. You might be wondering how a menu, if it's only a button, shows its menu items. The answer is that when a menu is activated, it automatically brings up a popup menu that displays the menu items.
The following code creates the menus shown near the beginning of this menu section. The bold lines of code create and connect the menu objects; the other code sets up or customizes the menu objects. You can find the entire program in MenuLookDemo.
Other required files are listed in the example index. Because this code has no event handling, the menus do nothing useful except to look as they should. If you run the example, you'll notice that despite the lack of custom event handling, menus and submenus appear when they should, and the check boxes and radio buttons respond appropriately when the user chooses them. Menu items, like other components, can be in at most one container. If you try to add a menu item to a second menu, the menu item will be removed from the first menu before being added to the second.
For a way of implementing multiple components that do the same thing, see How to Use Actions. Other methods in the preceding code include setAccelerator and setMnemonic , which are discussed a little later in Enabling Keyboard Operation.
To detect when the user chooses a JMenuItem , you can listen for action events just as you would for a JButton.
For examples of handling action and item events, see the button , radio button , and check box sections, as well as the list of examples at the end of this section.
Menus support two kinds of keyboard alternatives: mnemonics and accelerators. Mnemonics offer a way to use the keyboard to navigate the menu hierarchy, increasing the accessibility of programs. Accelerators , on the other hand, offer keyboard shortcuts to bypass navigating the menu hierarchy.
Mnemonics are for all users; accelerators are for power users. A mnemonic is a key that makes an already visible menu item be chosen. For example, in MenuDemo the first menu has the mnemonic A, and its second menu item has the mnemonic B.
This means that, when you run MenuDemo with the Java look and feel, pressing the Alt and A keys makes the first menu appear. While the first menu is visible, pressing the B key with or without Alt makes the second menu item be chosen. A menu item generally displays its mnemonic by underlining the first occurrence of the mnemonic character in the menu item's text, as the following snapshot shows.
An accelerator is a key combination that causes a menu item to be chosen, whether or not it's visible. For example, pressing the Alt and 2 keys in MenuDemo makes the first item in the first menu's submenu be chosen, without bringing up any menus.
The following snapshot shows how the Java look and feel displays a menu item that has an accelerator. You can specify a mnemonic either when constructing the menu item or with the setMnemonic method. To specify an accelerator, use the setAccelerator method. Here are examples of setting mnemonics and accelerators:. As you can see, you set a mnemonic by specifying the KeyEvent constant corresponding to the key the user should press. To specify an accelerator you must use a KeyStroke object, which combines a key specified by a KeyEvent constant and a modifier-key mask specified by an ActionEvent constant.
Because popup menus, unlike regular menus, aren't always contained by a component, accelerators in popup menu items don't work unless the popup menu is visible. To bring up a popup menu JPopupMenu , you must register a mouse listener on each component that the popup menu should be associated with.
The mouse listener must detect user requests that the popup menu be brought up. The exact gesture that should bring up a popup menu varies by look and feel. In Microsoft Windows, the user by convention brings up a popup menu by releasing the right mouse button while the cursor is over a component that is popup-enabled.
In the Java look and feel, the customary trigger is either pressing the right mouse button for a popup that goes away when the button is released or clicking it for a popup that stays up. Like Article. Attention reader! Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. Recommended Articles. Article Contributed By :.
Easy Normal Medium Hard Expert. Writing code in comment? Please use ide. Load Comments. What's New.
0コメント