by Dinesh Thakur Category: Swing

A menu bar represents a list of menus which can be added to the top of a top-level window. Each menu is associated with a drop-down list of menu items. The concept of menu bar can be implemented by using three Java classes: JMenuBar, Menu and JMenuitem. A menu bar is represented by the object of class JMenuBar in which only the default constructor is defined. A menu bar may consist of one or more menus represented by the objects of the class JMenu. A menu contains a list of menu items represented by the objects of the class JMenuitem.

The JMenu class defines the following constructors.

JMenu ()  //first

JMenu(String optionName) //second

JMenu(String optionName, boolean removable) //third

The first constructor creates an empty menu. The second constructor creates a menu with the name of the menu specified by optionName. In the third constructor, removable may have one of the two values: true or false. Ifit is true then the menu can float freely in the application window, otherwise it remains attached to the menu bar.

The JMenuitem class defines the following constructors.

JMenuitem () //first

JMenuitem(String itemName)       //second

JMenuitem(String itemName, Icon icon)  //third

The first constructor creates a menu item with no name and no menu shortcut. In the second constructor, itemName specifies the name of the menu item. In the third constructor, icon specifies the icon associated with the menu item.

A checkable menu item can also be created by using a subclass of JMenuitem called JCheckboxMenuitem.

The JCheckboxMenuitem class defines the following constructors.

JCheckboxMenuitem()

JCheckboxMenuitem(String itemName)

JCheckboxMenuitem(String itemName, boolean checkable)

The first constructor creates an unchecked menu item with no name. The second constructor creates an unchecked menu item with the name of the menu item specified by itemName. In the third constructor, checkable can either be true or false. If it is true then the menu item is initially checked, otherwise it is unchecked.

There are two types of menus which are given below.

• Regular menus: They are placed at the top of the application window within a menu bar.

• Pop-up menus: They appear in the window when the user clicks. For example, a pop-up menu appears on right click of the mouse.

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
public class JMenuBar2JavaExample extends JFrame
   {
         private JMenuBar MnuBar = new JMenuBar();
         private JMenu MnuOne = new JMenu("File");
         private JCheckBoxMenuItem chkOne = new  JCheckBoxMenuItem("Check Box First");
         private JCheckBoxMenuItem chkTwo = new JCheckBoxMenuItem("Check Box Second");
         private JRadioButtonMenuItem RdoOne = new JRadioButtonMenuItem("Radio Button One");
         private JRadioButtonMenuItem RdoTwo = new JRadioButtonMenuItem("Radio Button Two");
         private JRadioButtonMenuItem RdoThree = new JRadioButtonMenuItem("Radio Button Three");
         private ButtonGroup BtnGrp = new ButtonGroup();
         public JMenuBar2JavaExample()
         {
            setTitle("Menu Bar2 In java Swing");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLayout(new FlowLayout());
            setJMenuBar(MnuBar);
            MnuBar.add(MnuOne);
            MnuOne.add(chkOne);
            MnuOne.add(chkTwo);
            MnuOne.addSeparator();
            MnuOne.add(RdoOne);
            MnuOne.add(RdoTwo);
            MnuOne.add(RdoThree);
            BtnGrp.add(RdoOne);
            BtnGrp.add(RdoTwo);
            BtnGrp.add(RdoThree);
        }
            public static void main(String[] ds)
              {
                  JMenuBar2JavaExample frm = new JMenuBar2JavaExample();
                  final int WIDTH = 170;
                  final int HEIGHT = 220;
                  frm.setSize(500,500);
                  frm.setVisible(true);
             }
  }

MenuBar Example in Java Swing





About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles