Following is the declaration for javax.swing.JMenuItem class
public class JMenuItem
extends AbstractButton
implements Accessible, MenuElement
# Constructors
JMenuItem() | Creates a JMenuItem with no set text or icon. |
JMenuItem(Action a) | Creates a menu item whose properties are taken from the specified Action. |
JMenuItem(Icon icon) | Creates a JMenuItem with the specified icon. |
JMenuItem(String text) | Creates a JMenuItem with the specified text. |
JMenuItem(String text, Icon icon) | Creates a JMenuItem with the specified text and icon. |
JMenuItem(String text, int mnemonic) | Creates a JMenuItem with the specified text and keyboard mnemonic. |
# Methods
protected void actionPropertyChanged(Action action, String propertyName) | Updates the button’s state in response to property changes in the associated action. |
void addMenuDragMouseListener(MenuDragMouseListener l) | Adds a MenuDragMouseListener to the menu item. |
void addMenuKeyListener(MenuKeyListener l) | Adds a MenuKeyListener to the menu item. |
protected void configurePropertiesFromAction(Action a) | Sets the properties on this button to match those in the specified Action. |
protected void fireMenuDragMouseDragged(MenuDragMouseEvent event) | Notifies all listeners that have registered interest for notification on this event type. |
protected void fireMenuDragMouseEntered(MenuDragMouseEvent event) | Notifies all listeners that have registered interest for notification on this event type. |
protected void fireMenuDragMouseExited(MenuDragMouseEvent event) | Notifies all listeners that have registered interest for notification on this event type. |
protected void fireMenuDragMouseReleased(MenuDragMouseEvent event) | Notifies all listeners that have registered interest for notification on this event type. |
protected void fireMenuKeyPressed(MenuKeyEvent event) | Notifies all listeners that have registered interest for notification on this event type. |
protected void fireMenuKeyReleased(MenuKeyEvent event) | Notifies all listeners that have registered interest for notification on this event type. |
protected void fireMenuKeyTyped(MenuKeyEvent event) | Notifies all listeners that have registered interest for notification on this event type. |
KeyStroke getAccelerator() | Returns the KeyStroke which serves as an accelerator for the menu item. |
AccessibleContext getAccessibleContext() | Returns the AccessibleContext associated with this JMenuItem. |
Component getComponent() | Returns the java.awt.Component used to paint this object. |
MenuDragMouseListener[] getMenuDragMouseListeners() | Returns an array of all the MenuDragMouseListeners added to this JMenuItem with addMenuDragMouseListener(). |
MenuKeyListener[] getMenuKeyListeners() | Returns an array of all the MenuKeyListeners added to this JMenuItem with addMenuKeyListener(). |
MenuElement[] getSubElements() | This method returns an array containing the sub-menu components for this menu component. |
String getUIClassID() | Returns the suffix used to construct the name of the L&F class used to render this component. |
protected void init(String text, Icon icon) | Initializes the menu item with the specified text and icon. |
boolean isArmed() | Returns whether the menu item is “armed”. |
void menuSelectionChanged(boolean isIncluded) | Called by the MenuSelectionManager when the MenuElement is selected or unselected. |
protected String paramString() | Returns a string representation of this JMenuItem. |
void processKeyEvent(KeyEvent e, MenuElement[] path, MenuSelectionManager manager) | Processes a key event forwarded from the MenuSelectionManager and changes the menu selection, if necessary, by using MenuSelectionManager’s API. |
void processMenuDragMouseEvent(MenuDragMouseEvent e) | Handles mouse drag on a menu. |
void processMenuKeyEvent(MenuKeyEvent e) | Handles a keystroke in a menu. |
void processMouseEvent(MouseEvent e, MenuElement[] path, MenuSelectionManager manager) | Processes a mouse event forwarded from the MenuSelectionManager and changes the menu selection, if necessary, by using the MenuSelectionManager’s API. |
void removeMenuDragMouseListener(MenuDragMouseListener l) | Removes a MenuDragMouseListener from the menu item. |
void removeMenuKeyListener(MenuKeyListener l) | Removes a MenuKeyListener from the menu item. |
void setAccelerator(KeyStroke keyStroke) | Sets the key combination which invokes the menu item’s action listeners without navigating the menu hierarchy. |
void setArmed(boolean b) | Identifies the menu item as “armed”. |
void setEnabled(boolean b) | Enables or disables the menu item. |
void setModel(ButtonModel newModel) | Sets the model that this button represents. |
void setUI(MenuItemUI ui) | Sets the look and feel object that renders this component. |
void updateUI() | Resets the UI property with a value from the current look and feel. |
# Methods Inherited
- javax.swing.JAbstractButton
- javax.swing.JComponent
- java.awt.Container
- java.awt.Component
- java.lang.Object
# JMenuItem Example
SwingMenuDemo.java
package com.simplecheatsheet.gui;
import java.awt.*;
import java.awt.event.*;
public class SwingMenuDemo {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
public SwingMenuDemo(){
prepareGUI();
}
public static void main(String[] args){
SwingMenuDemo swingMenuDemo = new SwingMenuDemo();
swingMenuDemo.showMenuDemo();
}
private void prepareGUI(){
mainFrame = new JFrame("Java SWING Examples");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
headerLabel = new JLabel("",JLabel.CENTER );
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setSize(350,100);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showMenuDemo(){
//create a menu bar
final JMenuBar menuBar = new JMenuBar();
//create menus
JMenu fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");
final JMenu aboutMenu = new JMenu("About");
final JMenu linkMenu = new JMenu("Links");
//create menu items
JMenuItem newMenuItem = new JMenuItem("New");
newMenuItem.setMnemonic(KeyEvent.VK_N);
newMenuItem.setActionCommand("New");
JMenuItem openMenuItem = new JMenuItem("Open");
openMenuItem.setActionCommand("Open");
JMenuItem saveMenuItem = new JMenuItem("Save");
saveMenuItem.setActionCommand("Save");
JMenuItem exitMenuItem = new JMenuItem("Exit");
exitMenuItem.setActionCommand("Exit");
JMenuItem cutMenuItem = new JMenuItem("Cut");
cutMenuItem.setActionCommand("Cut");
JMenuItem copyMenuItem = new JMenuItem("Copy");
copyMenuItem.setActionCommand("Copy");
JMenuItem pasteMenuItem = new JMenuItem("Paste");
pasteMenuItem.setActionCommand("Paste");
MenuItemListener menuItemListener = new MenuItemListener();
newMenuItem.addActionListener(menuItemListener);
openMenuItem.addActionListener(menuItemListener);
saveMenuItem.addActionListener(menuItemListener);
exitMenuItem.addActionListener(menuItemListener);
cutMenuItem.addActionListener(menuItemListener);
copyMenuItem.addActionListener(menuItemListener);
pasteMenuItem.addActionListener(menuItemListener);
final JCheckBoxMenuItem showWindowMenu = new JCheckBoxMenuItem(
"Show About", true);
showWindowMenu.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(showWindowMenu.getState()){
menuBar.add(aboutMenu);
} else {
menuBar.remove(aboutMenu);
}
}
});
final JRadioButtonMenuItem showLinksMenu = new JRadioButtonMenuItem(
"Show Links", true);
showLinksMenu.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(menuBar.getMenu(3)!= null){
menuBar.remove(linkMenu);
mainFrame.repaint();
} else {
menuBar.add(linkMenu);
mainFrame.repaint();
}
}
});
//add menu items to menus
fileMenu.add(newMenuItem);
fileMenu.add(openMenuItem);
fileMenu.add(saveMenuItem);
fileMenu.addSeparator();
fileMenu.add(showWindowMenu);
fileMenu.addSeparator();
fileMenu.add(showLinksMenu);
fileMenu.addSeparator();
fileMenu.add(exitMenuItem);
editMenu.add(cutMenuItem);
editMenu.add(copyMenuItem);
editMenu.add(pasteMenuItem);
//add menu to menubar
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(aboutMenu);
menuBar.add(linkMenu);
//add menubar to the frame
mainFrame.setJMenuBar(menuBar);
mainFrame.setVisible(true);
}
class MenuItemListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
statusLabel.setText(e.getActionCommand() + " JMenuItem clicked.");
}
}
}