0% found this document useful (0 votes)
47 views

Jmenu Item

The document discusses Java Swing JMenuBar, JMenu, and JMenuItem components. It defines their constructors and commonly used methods. It then provides two example programs, one demonstrating adding menu items to a menu bar, and another adding menu items, submenu items, and action listeners.

Uploaded by

Tejas Shukla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Jmenu Item

The document discusses Java Swing JMenuBar, JMenu, and JMenuItem components. It defines their constructors and commonly used methods. It then provides two example programs, one demonstrating adding menu items to a menu bar, and another adding menu items, submenu items, and action listeners.

Uploaded by

Tejas Shukla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Dr Bharti Sharma

Java Swing | JMenuBar


 JMenuBar, JMenu and JMenuItems are a part of Java Swing package.
 JMenuBar is an implementation of menu bar .
 The JMenuBar contains one or more JMenu objects, when the JMenu
objects are selected they display a popup showing one or more
JMenuItems .
 JMenu basically represents a menu .
 It contains several JMenuItem Object . It may also contain JMenu Objects
(or submenu).

Constructors :

 JMenuBar() : Creates a new MenuBar.


 JMenu() : Creates a new Menu with no text.
 JMenu(String name) : Creates a new Menu with a specified name.
 JMenu(String name, boolean b) : Creates a new Menu with a specified
name and boolean value specifies it as a tear-off menu or not. A tear-off
menu can be opened and dragged away from its parent menu bar or
menu.

Commonly used methods:


add(JMenu c) : Adds menu to the menu bar. Adds JMenu object to the Menu
bar.
add(Component c) : Add component to the end of JMenu
add(Component c, int index) : Add component to the specified index of
JMenu
add(JMenuItem menuItem) : Adds menu item to the end of the menu.
add(String s) : Creates a menu item with specified string and appends it to the
end of menu.
getItem(int index) : Returns the specified menuitem at the given index

The following programs will illustrate the use of JMenuBar

1. Program to make a MenuBar and add menu items to it


 Java

// Java program to construct


// Menu bar to add menu items
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
Dr Bharti Sharma

public class menu extends JFrame {


// menubar
static JMenuBar mb;
// JMenu
static JMenu x;
// Menu items
static JMenuItem m1, m2, m3;
// create a frame
static JFrame f;
public static void main()
{
// create a frame
f = new JFrame("Menu demo");
// create a menubar
mb = new JMenuBar();
// create a menu
x = new JMenu("Menu");

// create menuitems
m1 = new JMenuItem("MenuItem1");
m2 = new JMenuItem("MenuItem2");
m3 = new JMenuItem("MenuItem3");

// add menu items to menu


x.add(m1);
x.add(m2);
x.add(m3);

// add menu to menu bar


mb.add(x);
// add menubar to frame
f.setJMenuBar(mb);
// set the size of the frame
f.setSize(500, 500);
f.setVisible(true);
}
}
Dr Bharti Sharma

Output :

2. Program to add a menubar and add menuitems, submenu items and


also add ActionListener to menu items

Java

// Java program to add a menubar


// and add menuitems, submenu items and also add
// ActionListener to menu items
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class menu1 extends JFrame implements ActionListener {
// menubar
static JMenuBar mb;

// JMenu
static JMenu x, x1;

// Menu items
static JMenuItem m1, m2, m3, s1, s2;

// create a frame
static JFrame f;

// a label
static JLabel l;
Dr Bharti Sharma

// main class
public static void main()
{
// create an object of the class
menu1 m = new menu1();

// create a frame
f = new JFrame("Menu demo");

// create a label
l = new JLabel("no task ");

// create a menubar
mb = new JMenuBar();

// create a menu
x = new JMenu("Menu");
x1 = new JMenu("submenu");

// create menuitems
m1 = new JMenuItem("MenuItem1");
m2 = new JMenuItem("MenuItem2");
m3 = new JMenuItem("MenuItem3");
s1 = new JMenuItem("SubMenuItem1");
s2 = new JMenuItem("SubMenuItem2");

// add ActionListener to menuItems


m1.addActionListener(m);
m2.addActionListener(m);
m3.addActionListener(m);
s1.addActionListener(m);
s2.addActionListener(m);

// add menu items to menu

x.add(m1);
x.add(m2);
x.add(m3);
x1.add(s1);
Dr Bharti Sharma

x1.add(s2);

// add submenu

x.add(x1);

// add menu to menu bar

mb.add(x);

// add menubar to frame

f.setJMenuBar(mb);

// add label

f.add(l);

// set the size of the frame

f.setSize(500, 500);

f.setVisible(true);

public void actionPerformed(ActionEvent e)

String s = e.getActionCommand();

// set the label to the menuItem that is selected

l.setText(s + " selected");


Dr Bharti Sharma

Output:

Note : The following program might not run in an online compiler, please use
an offline IDE.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy