Create Menu and Submenu in Menubar in Java

Q.: Write java application to create Menu and Submenu(Cascading menu) in menubar using swing and applet.

Compatibility/Version: Java 2 Platform Standard Edition Version 1.5 (Java 2 SDK, SE v1.5.0)

Concepts Used:

  • Applet
  • JMenuBar
  • JMenu
  • JApplet
  • setJMenuBar

Code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/*
<APPLET
    CODE = submenus.class
    WIDTH = 350
    HEIGHT = 280 >
</APPLET>
*/
public class submenus extends JApplet
{
    public void init()
    {
        JMenuBar jmenubar = new JMenuBar();

        JMenu jmenu  = new JMenu("Sub Menus");
        JMenu jsubmenu    = new JMenu("Cascading Menu");

        jmenu.add("Item 1");
        jmenu.add("Item 2");
        jmenu.add("Item 3");
        jmenu.add("Item 4");

        jsubmenu.add("Sub Item 1");
        jsubmenu.add("Sub Item 2");
        jsubmenu.add("Sub Item 3");
        jsubmenu.add("Sub Item 4");

        jmenu.add(jsubmenu);

        jmenubar.add(jmenu);

        setJMenuBar(jmenubar);
    }
}

Output:

Menubar with Menu and Submenu(Cascading Menu) in Java

Menubar with Menu and Submenu(Cascading Menu) in Java

Download Now (.Zip File – 18KB)…

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

CommentLuv badge

Human Verification: In order to verify that you are a human and not a spam bot, please enter the answer into the following box below based on the instructions contained in the graphic.