Following is the declaration for javax.swing.JMenu class
public class JMenu
extends JMenuItem
implements Accessible, MenuElement
# Field
Following is the field for java.awt.Component class
protected JMenu.WinListener popupListener | The window-closing listener for the popup. |
# Constructors
JMenu() | Constructs a new JMenu with no text. |
JMenu(Action a) | Constructs a menu whose properties are taken from the Action supplied. |
JMenu(String s) | Constructs a new JMenu with the supplied string as its text. |
JMenu(String s, boolean b) | Constructs a new JMenu with the supplied string as its text and specified as a tear-off menu or not. |
# Methods
JMenuItem add(Action a) | Creates a new menu item attached to the specified Action object and appends it to the end of this menu. |
Component add(Component c) | Appends a component to the end of this menu. |
Component add(Component c, int index) | Adds the specified component to this container at the given position. |
JMenuItem add(JMenuItem menuItem) | Appends a menu item to the end of this menu. |
JMenuItem add(String s) | Creates a new menu item with the specified text and appends it to the end of this menu. |
void addMenuListener(MenuListener l) | Adds a listener for menu events. |
void addSeparator() | Appends a new separator to the end of the menu. |
void applyComponentOrientation(ComponentOrientation o) | Sets the ComponentOrientation property of this menu and all components contained within it. |
protected PropertyChangeListener createActionChangeListener(JMenuItem b) | Returns a properly configured PropertyChangeListener which updates the control as changes to the Action occur. |
protected JMenuItem createActionComponent(Action a) | Factory method which creates the JMenuItem for Actions added to the JMenu. |
protected JMenu.WinListener createWinListener(JPopupMenu p) | Creates a window-closing listener for the popup. |
void doClick(int pressTime) | Programmatically performs a “click”. |
protected void fireMenuCanceled() | Notifies all listeners that have registered interest for notification on this event type. |
protected void fireMenuDeselected() | Notifies all listeners that have registered interest for notification on this event type. |
protected void fireMenuSelected() | Notifies all listeners that have registered interest for notification on this event type. |
AccessibleContext getAccessibleContext() | Gets the AccessibleContext associated with this JMenu. |
Component getComponent() | Returns the java.awt.Component used to paint this MenuElement. |
int getDelay() | Returns the suggested delay, in milliseconds, before submenus are popped up or down. |
JMenuItem getItem(int pos) | Returns the JMenuItem at the specified position. |
int getItemCount() | Returns the number of items on the menu, including separators. |
Component getMenuComponent(int n) | Returns the component at position n. |
int getMenuComponentCount() | Returns the number of components on the menu. |
Component[] getMenuComponents() | Returns an array of Components of the menu’s subcomponents. |
MenuListener[]getMenuListeners() | Returns an array of all the MenuListeners added to this JMenu with addMenuListener(). |
JPopupMenu getPopupMenu() | Returns the popup menu associated with this menu. |
protected Point getPopupMenuOrigin() | Computes the origin for the JMenu’s popup menu. |
MenuElement[] getSubElements() | Returns an array of MenuElements containing the submenu for this menu component. |
String getUIClassID() | Returns the name of the L&F class that renders this component. |
JMenuItem insert(Action a, int pos) | Inserts a new menu item attached to the specified Action object at a given position. |
| JMenuItem insert(JMenuItem mi, int pos) | Inserts the specified JMenuitem at a given position. |
void insert(String s, int pos) | Inserts a new menu item with the specified text at a given position. |
void insertSeparator(int index) | Inserts a separator at the specified position. |
boolean isMenuComponent(Component c) | Returns true, if the specified component exists in the submenu hierarchy. |
boolean isPopupMenuVisible() | Returns true, if the menu’s popup window is visible. |
boolean isSelected() | Returns true, if the menu is currently selected (highlighted). |
boolean isTearOff() | Returns true, if the menu can be torn off. |
boolean isTopLevelMenu() | Returns true, if the menu is a ‘top-level menu’, that is if it is the direct child of a menubar. |
void menuSelectionChanged(boolean isIncluded) | Message when the menubar selection changes to activate or deactivate this menu. |
protected String paramString() | Returns a string representation of this JMenu. |
protected void processKeyEvent(KeyEvent evt) | Processes keystroke events such as mnemonics and accelerators. |
void remove(Component c) | Removes the component c from this menu. |
void remove(int pos) | Removes the menu item at the specified index from this menu. |
void remove(JMenuItem item) | Removes the specified menu item from this menu. |
void removeAll() | Removes all menu items from this menu. |
void removeMenuListener(MenuListener l) | Removes a listener for menu events. |
void setAccelerator(KeyStroke keyStroke) | setAccelerator is not defined for JMenu. |
void setComponentOrientation(ComponentOrientation o) | Sets the language-sensitive orientation that is used to order the elements or text within this component. |
void setDelay(int d) | Sets the suggested delay before the menu’s PopupMenu is popped up or down. |
void setMenuLocation(int x, int y) | Sets the location of the popup component. |
void setModel(ButtonModel newModel) | Sets the data model for the “menu button” the label that the user clicks to open or close the menu. |
void setPopupMenuVisible(boolean b) | Sets the visibility of the menu’s popup. |
void setSelected(boolean b) | Sets the selection status of the menu. |
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
# JMenu 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.");
}
}
}