JMenu

java.lang.Object
 java.awt.Component
   java.awt.Container
       javax.swing.JComponent
           javax.swing.AbstractButton
               javax.swing.JMenuItem
                   javax.swing.JMenu
All Implemented Interfaces:
Accessible, ImageObserver, ItemSelectable, MenuContainer, MenuElement, Serializable, SwingConstants

An implementation of a menu — a popup window containing JMenuItems that is displayed when the user selects an item on the JMenuBar. In addition to JMenuItems, a JMenu can also contain JSeparators.

In essence, a menu is a button with an associated JPopupMenu. When the “button” is pressed, the JPopupMenu appears. If the “button” is on the JMenuBar, the menu is a top-level window. If the “button” is another menu item, then the JPopupMenu is “pull-right” menu.

Let us know develop a complete Menu example, MenuExample.java:

 

A Complete Menu Example

As you can see from the program output, we no introduce a lot of new concepts to the menu items.

The full code listing for MenuExample.java is:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class MenuExample
extends JFrame
implements ActionListener
{
private final int ITEM_PLAIN = 0; // Item types
private final int ITEM_CHECK = 1;
private final int ITEM_RADIO = 2;

private JPanel topPanel;
private JMenuBar menuBar;
private JMenu menuFile;
private JMenu menuEdit;
private JMenu menuProperty;
private JMenuItem menuPropertySystem;
private JMenuItem menuPropertyEditor;
private JMenuItem menuPropertyDisplay;
private JMenuItem menuFileNew;
private JMenuItem menuFileOpen;
private JMenuItem menuFileSave;
private JMenuItem menuFileSaveAs;
private JMenuItem menuFileExit;
private JMenuItem menuEditCopy;
private JMenuItem menuEditCut;
private JMenuItem menuEditPaste;

public MenuExample()
{
setTitle( “Complete Menu Application” );
setSize( 310, 130 );

topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );

// Create the menu bar
menuBar = new JMenuBar();

// Set this instance as the application’s menu bar
setJMenuBar( menuBar );

// Build the property sub-menu
menuProperty = new JMenu( “Properties” );
menuProperty.setMnemonic( ‘P’ );

// Create property items
menuPropertySystem = CreateMenuItem( menuProperty, ITEM_PLAIN,
“System…”, null, ‘S’, null );
menuPropertyEditor = CreateMenuItem( menuProperty, ITEM_PLAIN,
“Editor…”, null, ‘E’, null );
menuPropertyDisplay = CreateMenuItem( menuProperty, ITEM_PLAIN,
“Display…”, null, ‘D’, null );

// Create the file menu
menuFile = new JMenu( “File” );
menuFile.setMnemonic( ‘F’ );
menuBar.add( menuFile );

// Create the file menu
// Build a file menu items
menuFileNew = CreateMenuItem( menuFile, ITEM_PLAIN,
“New”, null, ‘N’, null );
menuFileOpen = CreateMenuItem( menuFile, ITEM_PLAIN, “Open…”,
new ImageIcon( “open.gif” ), ‘O’,
“Open a new file” );
menuFileSave = CreateMenuItem( menuFile, ITEM_PLAIN, “Save”,
new ImageIcon( “save.gif” ), ‘S’,
” Save this file” );
menuFileSaveAs = CreateMenuItem( menuFile, ITEM_PLAIN,
“Save As…”, null, ‘A’,
“Save this data to a new file” );
// Add the property menu
menuFile.addSeparator();
menuFile.add( menuProperty );
menuFile.addSeparator();
menuFileExit = CreateMenuItem( menuFile, ITEM_PLAIN,
“Exit”, null, ‘x’,
“Exit the program” );

// Create the file menu
menuEdit = new JMenu( “Edit” );
menuEdit.setMnemonic( ‘E’ );
menuBar.add( menuEdit );

// Create edit menu options
menuEditCut = CreateMenuItem( menuEdit, ITEM_PLAIN,
“Cut”, null, ‘t’,
“Cut data to the clipboard” );
menuEditCopy = CreateMenuItem( menuEdit, ITEM_PLAIN,
“Copy”, null, ‘C’,
“Copy data to the clipboard” );
menuEditPaste = CreateMenuItem( menuEdit, ITEM_PLAIN,
“Paste”, null, ‘P’,
“Paste data from the clipboard” );
}

public JMenuItem CreateMenuItem( JMenu menu, int iType, String sText,
ImageIcon image, int acceleratorKey,
String sToolTip )
{
// Create the item
JMenuItem menuItem;

switch( iType )
{
case ITEM_RADIO:
menuItem = new JRadioButtonMenuItem();
break;

case ITEM_CHECK:
menuItem = new JCheckBoxMenuItem();
break;

default:
menuItem = new JMenuItem();
break;
}

// Add the item test
menuItem.setText( sText );

// Add the optional icon
if( image != null )
menuItem.setIcon( image );

// Add the accelerator key
if( acceleratorKey > 0 )
menuItem.setMnemonic( acceleratorKey );

// Add the optional tool tip text
if( sToolTip != null )
menuItem.setToolTipText( sToolTip );

// Add an action handler to this menu item
menuItem.addActionListener( this );

menu.add( menuItem );

return menuItem;
}

public void actionPerformed( ActionEvent event )
{
System.out.println( event );
}

public static void main( String args[] )
{
// Create an instance of the test application
MenuExample mainFrame = new MenuExample();
mainFrame.setVisible( true );
}
}

The important features of this program are as follows:

  • This program is an expanded version of CascadeMenu.java.
  • Two Main menus exist File and Edit attached to the menuBar
  • Each have a Main menu menuFile and menuEdit attached.
  • The Edit menu is the less complex have three simple MenuItems: Cut, Copy and Paste. These have simple mnemonics and very basic functionality which we will address shortly.
  • The File menu has a Properties submenu (cascading menu) populated with some items as well having other items and separators itself.
  • Two of the File Menu items Open and Save have icons and text. The setIcon() method is used to include an ImageIcon with text.
  • A Tooltip is added to all menu items.
    • If you place the mouse over a menu item and wait a second a pop-up line of text will explain this function.
    • The method setToolTipText() is used for a given menu item instance.
    • The SToolTip parameter is passed in to the CreateMenuItem() method which is used to build all menu items in this example.
  • Keyboard mnemonics are set for all selections.
    • Specified as part of the CreateMenuItem() method
    • we pass a value to the parameter int acceleratorKey which is then used in the (Swing) method setMnemonic()
    • Associated with the acceleratorKey is the menu item text sText which MUST include the letter specified as the key.
    • Note that we must always have unique keys for items within the same menu. In the Edit menu we have Cut and Copy and we have to make sure we have different key choices.
  • When a menu item is selected the application must respond in some manner.
    • Menu items selected generate action events which can simply be intercepted as you would for a button.
    • Here, we simply print out an event message from a simple actionPerformed() method.
    • Clearly in proper applications we will need to have a variety of special actionPerformed() methods for each menu item.

You may also like...

1 Response

  1. kagiso says:

    add this code in the main method to make the application close when the close button is clicked.

    mainFrame.setDefaultCloseOperation(mainFrame.EXIT_ON_CLOSE);

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.