Working With JMenu in Swing


Introduction

In this article we are going to describe how you can create a menu in Swing. And how to trap the event of a particular menu item. Menu is class which inherits MenuItem class and two interfaces named MenuContainer and Accessible.Menubar deploy a menu object which is a drop-down menu component. It shows a list of menu choices. To implement this concept, we use the three classes MenuBar, Menu and MenuItem.
A menubar may contain more then one menu objects and each menu object contains a number of MenuItem objects. CheckboxMenuItem can also be used as a menu option. MenuComponent is an abstract class which inherits the Object class and implements the serializable interface. All these menu classes are basically the sub-class of MenuComponent, not Component. So they are placed on the container in a way different from other components such as buttons, labels, etc.The method setMenuBar() of the frame class is used to set the menu bar on the frame. A Menu bar conatins some shortcut keys for menu items. A menu item can have an instance of the MenuShorcut.Menu subclass which overrides the method and does not send any event to the frame until one of its subitems is selected.

Note A MenuBar cannot be placed on the Applet because they are not a subclass of the frame and they do not inherit the setMenuBar() method. So there is no way to place a MenuBar on the Applet.

Code for Creation

Step 1: Importing package

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

Step 2: Creating  menus and Add to the Menubar

JMenu file =
new JMenu("File");
file.setMnemonic(
'F');
file.add(menuItem(
"New", listener, "new", 'N', KeyEvent.VK_N));
file.add(menuItem(
"Open...", listener, "open", 'O', KeyEvent.VK_O));
file.add(menuItem(
"Save", listener, "save", 'S', KeyEvent.VK_S));
file.add(menuItem(
"Save As", listener, "saveas", 'A', KeyEvent.VK_A));
file.add(menuItem(
"Exit", listener, "Exit", '0', KeyEvent.VK_W));
JMenu edit =
new JMenu("Edit");
edit.setMnemonic(
'E');
edit.add(menuItem(
"Cut", listener, "cut", 0, KeyEvent.VK_X));
edit.add(menuItem(
"Copy", listener, "copy", 'C', KeyEvent.VK_C));
edit.add(menuItem(
"Paste", listener, "paste", 0, KeyEvent.VK_V));
JMenu format =
new JMenu("Format");
format.setMnemonic(
'E');
format.add(menuItem(
"Word Warp", listener, "ww", 0, KeyEvent.VK_X));
format.add(menuItem(
"Font", listener, "font", 'f', KeyEvent.VK_F));
JMenu help =
new JMenu("Help");
help.add(menuItem(
"Help", listener, "abhishek is made it", 0, KeyEvent.VK_H));
JMenuBar menubar =
new JMenuBar();
menubar.add(file);
menubar.add(edit);
menubar.add(format);
menubar.add(help);

Step 3: Creating frame and Panel and add Panel in to the frame.

JFrame frame = new JFrame(" Abhishek Menu Test");
JPanel panel =
new JPanel();
frame.getContentPane().add(panel,
"Center");

Step 4: Creating the Object own define class MenuItemActionListener.
ActionListener listener =
new MenuItemActionListener(panel);
frame.setJMenuBar(menubar);

Step 5: Set the size and make the frame visibility true.

frame.setSize(450, 300);

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 

Step 6: MenuItemActionListener is an event listener class used with the menu items created above. For this demo, it just displays a dialog box when an item is selected.
 

public static class MenuItemActionListener implements ActionListener

{

Component parent;

public MenuItemActionListener(Component parent)

{

this.parent = parent;

}

public void actionPerformed(ActionEvent e)

{

JMenuItem item = (JMenuItem) e.getSource();

String cmd = item.getActionCommand();

JOptionPane.showMessageDialog(parent," Now you click on" +cmd );

}

}


Step 7:
A convenience method for creating menu items.


public
static JMenuItem menuItem(String label,

ActionListener listener, String command,

int mnemonic, int acceleratorKey)

{

JMenuItem item = new JMenuItem(label);

item.addActionListener(listener);

item.setActionCommand(command);

if (mnemonic != 0) item.setMnemonic((char) mnemonic);

if (acceleratorKey != 0)

item.setAccelerator(KeyStroke.getKeyStroke(acceleratorKey,

java.awt.Event.CTRL_MASK));

return item;

}

Complete code

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class MenuTest

{

public static void main(String[] args)

{

JFrame frame = new JFrame(" Abhishek Menu Test");

JPanel panel = new JPanel();

frame.getContentPane().add(panel, "Center");

ActionListener listener = new MenuItemActionListener(panel);

JMenu file = new JMenu("File");

file.setMnemonic('F');

file.add(menuItem("New", listener, "new", 'N', KeyEvent.VK_N));

file.add(menuItem("Open...", listener, "open", 'O', KeyEvent.VK_O));

file.add(menuItem("Save", listener, "save", 'S', KeyEvent.VK_S));

file.add(menuItem("Save As", listener, "saveas", 'A', KeyEvent.VK_A));

file.add(menuItem("Exit", listener, "Exit", '0', KeyEvent.VK_W));

JMenu edit = new JMenu("Edit");

edit.setMnemonic('E');

edit.add(menuItem("Cut", listener, "cut", 0, KeyEvent.VK_X));

edit.add(menuItem("Copy", listener, "copy", 'C', KeyEvent.VK_C));

edit.add(menuItem("Paste", listener, "paste", 0, KeyEvent.VK_V));

JMenu format = new JMenu("Format");

format.setMnemonic('E');

format.add(menuItem("Word Warp", listener, "ww", 0, KeyEvent.VK_X));

format.add(menuItem("Font", listener, "font", 'f', KeyEvent.VK_F));

JMenu help = new JMenu("Help");

help.add(menuItem("Help", listener, "help", 0, KeyEvent.VK_H));

JMenuBar menubar = new JMenuBar();

menubar.add(file);

menubar.add(edit);

menubar.add(format);

menubar.add(help);

frame.setJMenuBar(menubar);

frame.setSize(450, 300);

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  }

public static JMenuItem menuItem(String label,

ActionListener listener, String command,

int mnemonic, int acceleratorKey)

{

JMenuItem item = new JMenuItem(label);

item.addActionListener(listener);

item.setActionCommand(command);

if (mnemonic != 0) item.setMnemonic((char) mnemonic);

if (acceleratorKey != 0)

item.setAccelerator(KeyStroke.getKeyStroke(acceleratorKey,

java.awt.Event.CTRL_MASK));

return item;

  }

public static class MenuItemActionListener implements ActionListener

{

Component parent;

public MenuItemActionListener(Component parent)

{

this.parent = parent;

}

public void actionPerformed(ActionEvent e)

{

JMenuItem item = (JMenuItem) e.getSource();

String cmd = item.getActionCommand();

JOptionPane.showMessageDialog(parent," Now you click on" +cmd );

}

}

}

 

Output:


 
Cmd output:

m00.jpg

Blank frame (initial look):


m01.jpg


Click on File menu:

m02.jpg


Click on File Edit menu:


m03.jpg


Click on File Format menu:


m04.jpg


Click on Help menu:


m05.jpg


When you click the open option within the file menu it shows a dailogbox with the following message:


m06.jpg


Resources


SharePoint Designer Work flow: Part I
SharePoint Designer Work flow: Part II
Three State Work flow in Sharepoint
Working with PHP Sessions
Working with PHP Include()

Up Next
    Ebook Download
    View all
    Learn
    View all