Home Java Example Java Swing Create menus and submenus in Java
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Create menus and submenus in Java

Advertisement
In this section, you will learn about creation of menus, submenus and Separators in Java Swing.

Create menus and submenus in Java

     

In this section, you will learn about creation of menus, submenus and Separators in Java Swing. Menu bar contains a collection of menus. Each menu can have multiple menu items these are called submenu. Similarly, all menus have multiples menu items. The Separator divides the menu items in a separate groups like same types of menu Items are divided into a individual parts. For pictorial representation, the image for the result of the given program is given below:

Submenus of the "File Menu"

Submenus of the "Edit Menu"

This program shows how to create menu bar, menus, submenus and Separators. Here, all items shows on a frame with the help of following methods and APIs:

JMenuBar:
This is the class which constructs a menu bar that contains several menus.

JMenu(String):
This is the constructor of JMenu class. This constructor constructs the new menu. It takes the string type value which is the name label for the menu.

JMenuItem(String):
This is the constructor of JMenuItem class which constructs new menu items for the specific menu. It takes string types value which is the label for the menu item.

JSeparator():
This is the constructor of JSeparator class which adds an extra line between menu items. This line, only separates the menu items.

setJMenuBar():
This method is used to set the menu bar to the specified frame. It takes the object of the JMenuBar class.

Here is the code of program:

import javax.swing.*;

public class SwingMenu{
  public static void main(String[] args) {
  SwingMenu s = new SwingMenu();
  }

  public SwingMenu(){
  JFrame frame = new JFrame("Creating a JMenuBar, JMenu, JMenuItem and 
seprator Component"
);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  JMenuBar menubar = new JMenuBar();
  JMenu filemenu = new JMenu("File");
  filemenu.add(new JSeparator());
  JMenu editmenu = new JMenu("Edit");
  editmenu.add(new JSeparator());
  JMenuItem fileItem1 = new JMenuItem("New");
  JMenuItem fileItem2 = new JMenuItem("Open");
  JMenuItem fileItem3 = new JMenuItem("Close");
  fileItem3.add(new JSeparator());
  JMenuItem fileItem4 = new JMenuItem("Save");
  JMenuItem editItem1 = new JMenuItem("Cut");
  JMenuItem editItem2 = new JMenuItem("Copy");
  editItem2.add(new JSeparator());
  JMenuItem editItem3 = new JMenuItem("Paste");
  JMenuItem editItem4 = new JMenuItem("Insert");
  filemenu.add(fileItem1);
  filemenu.add(fileItem2);
  filemenu.add(fileItem3);
  filemenu.add(fileItem4);
  editmenu.add(editItem1);
  editmenu.add(editItem2);
  editmenu.add(editItem3);
  editmenu.add(editItem4);
  menubar.add(filemenu);
  menubar.add(editmenu);
  frame.setJMenuBar(menubar);
  frame.setSize(400,400);
  frame.setVisible(true);
  }
}

Download this example

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 14, 2007

Related Tutorials

Ask Questions?    Discuss: Create menus and submenus in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
guptha
July 2, 2011
swing program

Create menus and submenus in Java in the above example when we click on open no new frame open how add the code to open new frame when click on open? can send code or suggestions to me thank u
guptha
July 2, 2011
swing program

Create menus and submenus in Java in the above example when we click on open no new frame open how to open a new frame when click on open. please send me code or suggestions to me thank u
jerung
August 15, 2012
submenu from menu

if i want to make submenu from that menu,,, mean: file > new > 1 . new data 2. new search > open > close
Amol
December 3, 2012
java

how to start system level project in java ?
mihratu
April 4, 2013
hi

you guy I know you are bullshet man .
Sabbir Jaman
June 20, 2013
Greetings

Fine example thank u from Bangladesh
aakash sapate
January 13, 2014
java and other programming

want to learn java
DMCA.com