Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

i want to have menubar smae as above example but with scrollbar or jscrollpane

Posted by rajesh on Thursday, 11.22.07 @ 13:47pm | #38300

Latest Searches:
command Sort Header on
Quartz
java program to transp
redirecting
jsp
out in jsp
difference between arr
sample
java ????¢Ñ????? ?????
javaclient/serverprogr
static
actionperformed
insert data tohtml fil
clipiing source code i
html to pdf
GetCharFromString usin
how to write in pdf us
j2me datefield
errors
clear values from a fo
write a xml file using
struts supported jar f
session.set attribute(
append message
types of heap sort
sort object compareTo
call javascript throug
File
using the selection so
DefaultMutableTreeNode
floating point
<select><option> jsp d
changehashtablevalue
Netbeans JSF
JSP:File Field
events in jsp
string variable
Create Scroll Bar in J
netbens jsf
call name in java usin
java keyword
JSLT Example
create excel using in
how to iterate the str
Jmenu in java
stopwacth
facelets
daouble in java
.setDocument(new
view record by charact
Combo Box operation in
storing text data to a
browse an image from h
Java example program t
WebObjects
treeview structure cod
java compiler/
Convert Time to Second
is action class is sin
parseInt
jsp bean get property
phasesofjavaprogram
GetCharFromString usin
two dimensional array
algorithm for area and
Sort in Java
credit card transactio
JScrollPane
adding image in j2ee
enabling and disabling
jbossconnectionpool
convert unicode charac
inno
sourse code for add tw
page refresh
refid in sql select st
JDBC and Mysql
JSTL : Submit Form Tex
import java.io.*;
PLSQL
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.