by Dinesh Thakur Category: Swing

We understand all upper menu structure of a graphical application that allows us to display a list of operations that in turn can contain other lists of operations. The basic structure consists of a menu JMenuBar (the top bar) from which dangle (JMenu) menus that we add elements (JMenuItem) that can be JCheckBoxMenuItem, and even JRadioButtonMenuItem JMenu. Consider now the methods of each of these classes:


public JMenuBar (): creates a new JMenuBar;
public JMenu add (JMenu m): add a JMenu to the MenuBar;
GetMenu public JMenu (int index): Returns the index associated with the corresponding menu.


JMenuItem (JCheckBoxMenuItem superclass, and JradioButtonItem JMenu):
public JMenuItem () creates an empty menu item;
public JMenuItem (String text) creates a string associated;
public JMenuItem (Icon icon) is created with an associated image that will be displayed in the menu;
public void setEnabled (boolean b) indicate whether we want or not can be selected.



public JMenu (String s) Creates a new JMenu that displays the specified string;
public JMenu add (JMenuItem menuItem): adds the JMenu JMenuItem;
public JMenu add (String s) Creates a new menu item with the specified text and puts it at the end of this JMenu;
public void addSeparator () adds a separating horizontal line;
public JMenuItem getItem (int pos): Returns the JMenuItem containing the indicated position

import java.awt.*;

import javax.swing.*;
import java.awt.event.*;
public class JavaExampleMenuControlInJApplet extends JApplet implements ActionListener
    public void init()
        JMenuBar MnuBar = new JMenuBar();
        JMenu MnuOne = new JMenu("File");
        JMenuItem MnuItmOne = new JMenuItem("New..."),
        MnuItmTwo = new JMenuItem("Open..."),
        MnuItmThree = new JMenuItem("Exit");
        JButton BtnPrss = new JButton("Prees Here!");
        BtnPrss.setActionCommand("You Pressed the Button");
        JMenu MnuTwo = new JMenu("Edit");
        JMenuItem MnuItmFour = new JMenuItem("Cut"),
        MnuItmFive = new JMenuItem("Copy"),
        MnuItmSix = new JMenuItem("Paste");
        public void actionPerformed(ActionEvent e1)
            JButton Btn = (JButton)e1.getSource();
/*<APPLET CODE=JavaExampleMenuControlInJApplet.class  WIDTH = 350  HEIGHT = 280 ></APPLET>*/

Menu Control in Java Swing Example

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.

Search Content

Popular Article