6.) Menus
 
Menus are created with the JMenuBar, JMenu, and JMenuItem classes. Hot keys and sub menus can be created with these classes. The idea is first you create a menu bar, then you create menu objects to add to the menu bar. Also to fill in the menus, menu items are created to be added to the menus.
To illustrate a simple window with two menus consider the following example:

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

class testMenu {

    // simple program to create a window with a menu

    public static void main(String[] args) {

        JFrame X = new JFrame("A Simple Window");

        // set the x,y position, width and height
        X.setBounds(25,80,350,200);

        // set the default close operation
        X.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // first create a menubar

        JMenuBar menuBar = new JMenuBar();

        // create each item on the menu bar
        JMenu fileMenu = new JMenu("File");
        JMenu otherMenu = new JMenu("Other");

        // add the menu objects to the menu bar
        menuBar.add(fileMenu);
        menuBar.add(otherMenu);

        X.setJMenuBar(menuBar);

        X.setVisible(true);    // shows the window
    }
}
Output:


A simple menu bar with only two menus, File and Other. Now consider adding menu items to the File menu as shown below:
import java.awt.*;
import javax.swing.*;

class testMenu2 {

    // simple program to create a window with a menu

    public static void main(String[] args) {

        JFrame X = new JFrame("A Simple Window");

        // set the x,y position, width and height
        X.setBounds(25,80,350,200);

        // set the default close operation
        X.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        // first create a menubar

        JMenuBar menuBar = new JMenuBar();

        // create each item on the menu bar
        JMenu fileMenu = new JMenu("File");
        JMenu otherMenu = new JMenu("Other");

        // create menu item
        JMenuItem item = new JMenuItem("New");

        // add item to filemenu
        fileMenu.add(item);

        item = new JMenuItem("Open");
        fileMenu.add(item);

        fileMenu.addSeparator();

        item = new JMenuItem("Exit");
        fileMenu.add(item);

        // add the menu objects to the menu bar
        menuBar.add(fileMenu);
        menuBar.add(otherMenu);

        X.setJMenuBar(menuBar);

        X.setVisible(true);    // shows the window

    }
}
Output:


A Menu can have a sub-menu, consider adding a sub menu to the previous example as shown below:
import java.awt.*;
import javax.swing.*;

class testMenu3 {

    // simple program to create a window with a menu

    public static void main(String[] args) {

        JFrame X = new JFrame("A Simple Window");

        // set the x,y position, width and height
        X.setBounds(25,80,350,200);

        // set the default close operation
        X.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        // first create a menubar

        JMenuBar menuBar = new JMenuBar();

        // create each item on the menu bar
        JMenu fileMenu = new JMenu("File");
        JMenu otherMenu = new JMenu("Other");

        // create menu item
        JMenuItem item = new JMenuItem("New");

        // add item to filemenu
        fileMenu.add(item);

        item = new JMenuItem("Open");
        fileMenu.add(item);

        fileMenu.addSeparator();

        // create a submenu
        JMenu submenu = new JMenu("Sub Menu");

        item = new JMenuItem("Option 1");
        submenu.add(item);

        item = new JMenuItem("Option 2");
        submenu.add(item);

        fileMenu.add(submenu);

        fileMenu.addSeparator();

        item = new JMenuItem("Exit");
        fileMenu.add(item);

        // add the menu objects to the menu bar
        menuBar.add(fileMenu);
        menuBar.add(otherMenu);

        X.setJMenuBar(menuBar);

        X.setVisible(true);    // shows the window

    }
}
Output:


A Menu can have short cut keys. The example below shows how to add short-cut keys to menus and menu items:
import java.awt.*;
import javax.swing.*;

class testMenu4 {

    // simple program to create a window with a menu
    // with short cut keys

    public static void main(String[] args) {

        JFrame X = new JFrame("A Simple Window");

        // set the x,y position, width and height
        X.setBounds(25,80,350,200);

        // set the default close operation
        X.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        // first create a menubar

        JMenuBar menuBar = new JMenuBar();

        // create each item on the menu bar
        JMenu fileMenu = new JMenu("File");
        JMenu otherMenu = new JMenu("Other");

        // shortcuts for menu
        fileMenu.setMnemonic('F');
        otherMenu.setMnemonic('O');

        // create menu item
        JMenuItem item = new JMenuItem("New");

        // short cut for menu item
        item.setAccelerator(KeyStroke.getKeyStroke('N',Event.CTRL_MASK));

        // add item to filemenu
        fileMenu.add(item);

        item = new JMenuItem("Open");
         // short cut for menu item
        item.setAccelerator(KeyStroke.getKeyStroke('O',Event.CTRL_MASK));

        fileMenu.add(item);

        fileMenu.addSeparator();

        item = new JMenuItem("Exit");
         // short cut for menu item
        item.setAccelerator(KeyStroke.getKeyStroke('X',Event.CTRL_MASK));
        fileMenu.add(item);

        // add the menu objects to the menu bar
        menuBar.add(fileMenu);
        menuBar.add(otherMenu);

        X.setJMenuBar(menuBar);

        X.setVisible(true);    // shows the window

    }
}
Output:


Next
Back to Main