Menu components in Swing are subclasses of JComponent so they have all functionality of a normal Swing component.
Here are some remarkable characteristics of the Java Swing menu:
- Menu items can have both icons and text.
- Menu items can be checkboxes or radio buttons.
- Each menu item can have one keyboard accelerator that display after the menu item text.
- Almost standard Swing components can be used as menu items.
Here is the class hierarchy of Swing menu system.
Create Menu Items with JMenuItem
In order to create menu items in Swing, you need to create new instances of JMenuItem and set different properties for them. You can create menu item with both text and icon.
Here are the constructors of JMenuItem:
Constructors | Description |
---|---|
JMenuItem() | Creates a JMenuItem instance without icon or text. |
JMenuItem(Icon icon) | Creates a JMenuItem instance with a given icon. |
JMenuItem(String text) | Creates a JMenuItem instance with a given text. |
JMenuItem(String text, Icon icon) | Creates a JMenuItem instance with a given text and icon |
JMenuItem(String text, int mnemonic) | Creates a JMenuItem instance with the given text and keyboard mnemonic. |
JMenuItem(Action a) | Creates a JMenuItem instance whose properties are taken from the a given Action. |
Here is the code snippet to create a menu item:
menuItem = new JMenuItem("New Project...",
new ImageIcon("images/newproject.png"));
menuItem.setMnemonic(KeyEvent.VK_P);
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_1, ActionEvent.ALT_MASK));
menuItem.getAccessibleContext().setAccessibleDescription(
"New Project");
Code language: JavaScript (javascript)
Create Menu with JMenu
After having menu items, you need to add them to a menu. In order to create a menu, you need to use JMenu class. JMenu class represents the menu that can attach to a menu bar or another menu. A menu directly attached to a menu bar is known as a top-level menu. If the menu is attached to a menu, it is called a sub-menu.
Here are the constructors of JMenu class:
Constructors | Description |
---|---|
JMenu() | Creates an instance of JMenu without text. |
JMenu(String s) | Creates an instance of JMenu a given text. |
JMenu(String s, boolean tearOffMenu) | Creates an instance of JMenu a given text. and specify the menu as a tear-off menu or not. |
JMenu(Action a) | Creates an instance of JMenu whose properties are taken from the specified Action. |
Here is the code snippet to create a menu and add a menu item to that menu.
//Build the File Menu.
menu = new JMenu("File");
menu.setMnemonic(KeyEvent.VK_F);
menu.getAccessibleContext().setAccessibleDescription("Dealing with Files");
// create menu item and add it to the menu
menuItem = new JMenuItem("New File...",
new ImageIcon("images/newfile.png"));
menuItem.setMnemonic(KeyEvent.VK_F);
menu.add(menuItem);
Code language: JavaScript (javascript)
Create menu bar and attach it to a frame
JMenuBar class represents menu bar and can attach it to a frame. One or more menu can be added to a menu bar by using add() method. To attach menu bar to a frame you use method setJMenuBar() of frame.
Here is the menu demo application:
package jmenudemo;
import javax.swing.*;
import java.awt.event.*;
public class Main {
public static JMenuBar createMenuBar() {
JMenuBar menuBar;
JMenu menu, submenu;
JMenuItem menuItem;
JRadioButtonMenuItem rdmi;
JCheckBoxMenuItem cbmi;
//Create the menu bar.
menuBar = new JMenuBar();
//Build the File Menu.
menu = new JMenu("File");
menu.setMnemonic(KeyEvent.VK_F);
menu.getAccessibleContext().setAccessibleDescription("Dealing with Files");
menuBar.add(menu);
//a group of JMenuItems
menuItem = new JMenuItem("New Project...",
new ImageIcon("images/newproject.png"));
menuItem.setMnemonic(KeyEvent.VK_P);
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_1, ActionEvent.ALT_MASK));
menuItem.getAccessibleContext().setAccessibleDescription(
"New Project");
menu.add(menuItem);
menuItem = new JMenuItem("New File...",
new ImageIcon("images/newfile.png"));
menuItem.setMnemonic(KeyEvent.VK_F);
menu.add(menuItem);
//a group of check box menu items
menu.addSeparator();
cbmi = new JCheckBoxMenuItem("A check box menu item");
cbmi.setMnemonic(KeyEvent.VK_C);
menu.add(cbmi);
cbmi = new JCheckBoxMenuItem("Another one");
cbmi.setMnemonic(KeyEvent.VK_H);
menu.add(cbmi);
//a group of radio button menu items
menu.addSeparator();
ButtonGroup group = new ButtonGroup();
rdmi = new JRadioButtonMenuItem("Radio button menu item");
rdmi.setSelected(true);
rdmi.setMnemonic(KeyEvent.VK_R);
group.add(rdmi);
menu.add(rdmi);
rdmi = new JRadioButtonMenuItem("Another radio button");
rdmi.setMnemonic(KeyEvent.VK_O);
group.add(rdmi);
menu.add(rdmi);
//a submenu
menu.addSeparator();
submenu = new JMenu("A submenu");
submenu.setMnemonic(KeyEvent.VK_S);
menuItem = new JMenuItem("Menu item in the submenu");
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_2, ActionEvent.ALT_MASK));
submenu.add(menuItem);
menuItem = new JMenuItem("Another menu item");
submenu.add(menuItem);
menu.add(submenu);
//Build Edit menu in the menu bar.
menu = new JMenu("Edit");
menu.setMnemonic(KeyEvent.VK_E);
menu.getAccessibleContext().setAccessibleDescription(
"Edit Menu");
menuBar.add(menu);
return menuBar;
}
public static void main(String[] args) {
final JFrame frame = new JFrame("Menu Demo");
frame.setJMenuBar(createMenuBar());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 300);
frame.setVisible(true);
}
}
Code language: JavaScript (javascript)