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
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class menu extends JFrame {
static JMenuBar mb;
static JMenu x;
static JMenuItem m1, m2, m3;
static JFrame f;
public static void main()
{
f = new JFrame("Menu demo");
mb = new JMenuBar();
x = new JMenu("Menu");
m1 = new JMenuItem("MenuItem1");
m2 = new JMenuItem("MenuItem2");
m3 = new JMenuItem("MenuItem3");
x.add(m1);
x.add(m2);
x.add(m3);
mb.add(x);
f.setJMenuBar(mb);
f.setSize( 500 , 500 );
f.setVisible( true );
}
}
|
Output :
2. Program to add a menubar and add menuitems, submenu items and also add ActionListener to menu items
Java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class menu1 extends JFrame implements ActionListener {
static JMenuBar mb;
static JMenu x, x1;
static JMenuItem m1, m2, m3, s1, s2;
static JFrame f;
static JLabel l;
public static void main()
{
menu1 m = new menu1();
f = new JFrame("Menu demo");
l = new JLabel("no task ");
mb = new JMenuBar();
x = new JMenu("Menu");
x1 = new JMenu("submenu");
m1 = new JMenuItem("MenuItem1");
m2 = new JMenuItem("MenuItem2");
m3 = new JMenuItem("MenuItem3");
s1 = new JMenuItem("SubMenuItem1");
s2 = new JMenuItem("SubMenuItem2");
m1.addActionListener(m);
m2.addActionListener(m);
m3.addActionListener(m);
s1.addActionListener(m);
s2.addActionListener(m);
x.add(m1);
x.add(m2);
x.add(m3);
x1.add(s1);
x1.add(s2);
x.add(x1);
mb.add(x);
f.setJMenuBar(mb);
f.add(l);
f.setSize( 500 , 500 );
f.setVisible( true );
}
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
l.setText(s + " selected");
}
}
|
Output:
Note : The following program might not run in an online compiler, please use an offline IDE.
Feeling lost in the vast world of Backend Development? It's time for a change! Join our
Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Last Updated :
20 May, 2022
Like Article
Save Article