Swing menus and menu items are buttons, because jmenuitem extends javasactbutton and jmenu extends jmenuitem. Therefore, swing menus and menu items inherit the following functions, including text and (or) icons, icons that display the cursor when entering, and mnemonic.
Parent components of jmenuitem and jmenu, which are finally attributed to Java. AWT. container to emphasize that both menu items and menu items are containers. You can add any type of components (from an animated GIF tag to an instance of the jtree) to a menu or menu item.
Menus and menu items also implement the menuelement interface to participate in the processing of menu events.
The tool bar and pop-up menu both extend the jcomponent class, that is, any component can be added to a tool bar or pop-up menu.
This chapter describes the following components:
• Jmenuitem
• Jcheckboxmenuitem
• Jradiobuttonmenuitem
• Jmenu
• Jpopupmenu
• Jmenubar
Example 1: Add a check menu to the menu
import java.awt.event.KeyEvent; import javax.swing.JCheckBoxMenuItem; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; public class JCheckBoxMenuItemDemo { public static void main(final String args[]) { JFrame frame = new JFrame("MenuSample Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JMenuBar menuBar = new JMenuBar(); // File Menu, F - Mnemonic JMenu fileMenu = new JMenu("File"); fileMenu.setMnemonic(KeyEvent.VK_F); menuBar.add(fileMenu); // File->New, N - Mnemonic JMenuItem newMenuItem = new JMenuItem("New", KeyEvent.VK_N); fileMenu.add(newMenuItem); JCheckBoxMenuItem caseMenuItem = new JCheckBoxMenuItem("Case Sensitive"); caseMenuItem.setMnemonic(KeyEvent.VK_C); fileMenu.add(caseMenuItem); frame.setJMenuBar(menuBar); frame.setSize(350, 250); frame.setVisible(true); } }
Example 2: menu event processing
import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.AbstractButton; import javax.swing.ButtonGroup; import javax.swing.JCheckBoxMenuItem; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JRadioButtonMenuItem; import javax.swing.JSeparator; import javax.swing.JTextField; public class SwingWinMenus extends JFrame { public static int WIDTH = 300; public static int HEIGHT = 300; public static String TITLE = "SwingWin"; Container frameContainer; JTextField textField = new JTextField(50); JMenuBar menuBar = new JMenuBar(); JMenu fileMenu = new JMenu("File"); JMenuItem fileNew = new JMenuItem("New"); JMenuItem fileOpen = new JMenuItem("Open"); JMenuItem fileSave = new JMenuItem("Save"); JMenuItem fileExit = new JMenuItem("Exit"); JMenu editMenu = new JMenu("Edit"); JMenuItem editCut = new JMenuItem("Cut"); JMenuItem editCopy = new JMenuItem("Copy"); JMenuItem editPaste = new JMenuItem("Paste"); JMenu specialMenu = new JMenu("Special"); JCheckBoxMenuItem specialCheck1 = new JCheckBoxMenuItem("Check 1"); JCheckBoxMenuItem specialCheck2 = new JCheckBoxMenuItem("Check 2", true); JSeparator separator = new JSeparator(); JRadioButtonMenuItem specialRadio1 = new JRadioButtonMenuItem("Radio 1"); JRadioButtonMenuItem specialRadio2 = new JRadioButtonMenuItem("Radio 2"); ButtonGroup buttonGroup = new ButtonGroup(); public SwingWinMenus() { super(""); fileMenu.add(fileNew); fileMenu.add(fileOpen); fileMenu.add(fileSave); fileMenu.add(fileExit); editMenu.add(editCut); editMenu.add(editCopy); editMenu.add(editPaste); specialMenu.add(specialCheck1); specialMenu.add(specialCheck2); specialMenu.add(separator); buttonGroup.add(specialRadio1); buttonGroup.add(specialRadio2); specialMenu.add(specialRadio1); specialMenu.add(specialRadio2); menuBar.add(fileMenu); menuBar.add(editMenu); menuBar.add(specialMenu); setJMenuBar(menuBar); frameContainer = getContentPane(); frameContainer.setLayout(null); textField.setBounds(100, 100, 100, 20); frameContainer.add(textField); setupEventHandlers(); setSize(450, 450); setVisible(true); } void setupEventHandlers() { addWindowListener(new WindowHandler()); fileNew.addActionListener(new MenuItemHandler()); fileOpen.addActionListener(new MenuItemHandler()); fileSave.addActionListener(new MenuItemHandler()); fileExit.addActionListener(new MenuItemHandler()); editCut.addActionListener(new MenuItemHandler()); editCopy.addActionListener(new MenuItemHandler()); editPaste.addActionListener(new MenuItemHandler()); specialCheck1.addItemListener(new ItemHandler()); specialCheck2.addItemListener(new ItemHandler()); specialRadio1.addItemListener(new ItemHandler()); specialRadio2.addItemListener(new ItemHandler()); } public static void main(String[] args) { SwingWinMenus app = new SwingWinMenus(); } public class WindowHandler extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } public class MenuItemHandler implements ActionListener { public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); if (cmd.equals("Exit")) System.exit(0); else textField.setText(cmd); } } public class ItemHandler implements ItemListener { public void itemStateChanged(ItemEvent e) { AbstractButton button = (AbstractButton) e.getItem(); String label = button.getText(); if (button.isSelected()) label += " true"; else label += " false"; textField.setText(label); } } }
Example 3: The test menu is displayed.
import java.awt.Dimension; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JMenuItem; import javax.swing.JPopupMenu; import javax.swing.event.PopupMenuEvent; import javax.swing.event.PopupMenuListener; // Define PopupMenuListener class MyPopupMenuListener implements PopupMenuListener { public void popupMenuCanceled(PopupMenuEvent popupMenuEvent) { System.out.println("Canceled"); } public void popupMenuWillBecomeInvisible(PopupMenuEvent popupMenuEvent) { System.out.println("Becoming Invisible"); } public void popupMenuWillBecomeVisible(PopupMenuEvent popupMenuEvent) { System.out.println("Becoming Visible"); } } public class PopupMenuListenerDemo { public static void main(final String args[]) { JFrame frame = new JFrame("PopupSample Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Create popup menu, attach popup menu listener JPopupMenu popupMenu = new JPopupMenu("Title"); popupMenu.setSize(new Dimension(100,50)); PopupMenuListener popupMenuListener = new MyPopupMenuListener(); popupMenu.addPopupMenuListener(popupMenuListener); // Cut JMenuItem cutMenuItem = new JMenuItem("Cut"); popupMenu.add(cutMenuItem); // Copy JMenuItem copyMenuItem = new JMenuItem("Copy"); popupMenu.add(copyMenuItem); // Paste JMenuItem pasteMenuItem = new JMenuItem("Paste"); pasteMenuItem.setEnabled(false); popupMenu.add(pasteMenuItem); // Separator popupMenu.addSeparator(); // Find JMenuItem findMenuItem = new JMenuItem("Find"); popupMenu.add(findMenuItem); JButton label = new JButton(); frame.add(label); label.setComponentPopupMenu(popupMenu); frame.setSize(350, 250); frame.setVisible(true); } }
Example 4: Add shortcuts to the menu
import java.awt.event.KeyEvent; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.KeyStroke; public class JMenuItemKeyStroke { public static void main(final String args[]) { JFrame frame = new JFrame("MenuSample Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JMenuBar menuBar = new JMenuBar(); // File Menu, F - Mnemonic JMenu fileMenu = new JMenu("File"); fileMenu.setMnemonic(KeyEvent.VK_F); menuBar.add(fileMenu); // File->New, N - Mnemonic JMenuItem newMenuItem = new JMenuItem("New", KeyEvent.VK_N); fileMenu.add(newMenuItem); // Edit->Cut, T - Mnemonic, CTRL-X - Accelerator JMenuItem cutMenuItem = new JMenuItem("Cut", KeyEvent.VK_T); KeyStroke ctrlXKeyStroke = KeyStroke.getKeyStroke("control X"); cutMenuItem.setAccelerator(ctrlXKeyStroke); fileMenu.add(cutMenuItem); frame.setJMenuBar(menuBar); frame.setSize(350, 250); frame.setVisible(true); } }