Updated March 27, 2023
Introduction to JMenu
JMenu, JMenuBar, and JMenuItems are a piece of Java Swing bundle. JMenuBar is an execution of a menu bar. The JMenuBar contains at least one JMenu objects. When these objects are chosen they show a popup demonstrating at least one JMenuItems. It essentially speaks to a menu. It contains a few JMenuItem Objects. It might likewise contain JMenu Objects (or submenu). The Menu class speaks to the drawdown menu part which is sent from a menu bar. The JMenuItem class speaks to the real thing in a menu. All things in a menu ought to get from class JMenuItem or one of its subclasses.
Syntax:
1. Class Declaration (JMenuBar)
public class JMenuBar extends JComponent implements MenuElement, Accessible
2. Class Declaration (JMenu)
public class JMenu extends JMenuItem implements MenuElement, Accessible
3. Class Declaration (JMenuItem)
public class JMenuItem extends AbstractButton implements Accessible, MenuElement
4. The syntax to make a JMenuBar in Java Swing
JMenuBar <JMenuBar_name>=new JMenuBar ();
5. The syntax to make a JMenuBar at the highest point of the holder
setJMenuBar (<JMenuBar_name>);
6. The syntax structure to make a different menu and add the menu to the JMenuBar
JMenu <JMenu_name>=new JMenu ("menu name");
<JMenuBar_name>. add(<JMenu_name>);
7. The syntax to make menu items
JMenuItem <JMenuItem_name>=new JMenuItem ("menu name ");
Constructors of JMenu
Following are the different constructors:
1. JMenuBar (): It will make another MenuBar.
Syntax:
public JMenu ()
import javax.swing.JMenu; (Import from swing JMenu)
import javax.swing.JMenuBar; (Import from swing JMenuBar)
import javax.swing.JMenuItem; (Import from swing JMenuItem)
2. JMenu (): It will create and make another Menu with no content.
Syntax:
public JMenu ()
import javax.swing.JMenu; (Import from swing JMenu)
import javax.swing.JMenuBar; (Import from swing JMenuBar)
import javax.swing.JMenuItem; (Import from swing JMenuItem)
3. JMenu (String name): Makes another Menu with a predetermined name.
Syntax:
public JMenu (String s)
import javax.swing.JMenu; (Import from swing JMenu)
import javax.swing.JMenuBar; (Import from swing JMenuBar)
import javax.swing.JMenuItem; (Import from swing JMenuItem)
4. JMenu (String name, Boolean b): Creates another Menu with a predetermined name and Boolean esteem determines it as a remove menu or not. A detach menu can be opened and hauled away from its parent menu bar or menu.
Syntax:
public JMenu (String s, boolean x)
import javax.swing.JMenu; (Import from swing JMenu)
import javax.swing.JMenuBar; (Import from swing JMenuBar)
import javax.swing.JMenuItem; (Import from swing JMenuItem)
Ordinarily Utilized Techniques:
- add (JMenu c): Adds menu to the menu bar. Adds JMenu item to the Menu bar.
- add (Component c): Add part to the finish of JMenu·
- add (Component c, int list): Add segment to the predefined list of JMenu·
- add (JMenuItem menuItem): Adds menu thing as far as possible of the menu·
- add (String s): Creates a menu thing with indicated string and adds it as far as possible of the menu.
Examples
Given below are the examples:
Example #1
Java code to build Menu bar to add menu items.
Code:
import java.awt.*;
import javax.swing.*;
import java.awt. event. *;
public class menu extends JFrame {
// Menu Bar declaration
static JMenuBar xb;
// JMenu declaration
static JMenu x;
// Menu items
static JMenuItem x1, x2, x3;
// frame creation declaration
static JFrame y;
public static void main()
{
// frame creation using new keyword
y = new JFrame ("Menu Demo");
// menu bar creation using new keyword
mb = new JMenuBar ();
// menu creation using new keyword
z = new JMenu("Menu");
// menu item creation using new keyword
x1= new JMenuItem("MenuItem1");
x2= new JMenuItem("MenuItem2");
x2= new JMenuItem("MenuItem3");
// add menu items to menu
x.add(x1);
x.add(x2);
x.add(x3);
// add menu to menu bar
xb.add(z);
// add menubar to frame
y.setJMenuBar(xb);
// set the size of the frame
y.setSize(500, 500);
y.setVisible(true);
}
}
Output:
Example #2
Java code to include a menu bar and include menuitems, submenu things and furthermore add ActionListener to menu things.
Code:
public class menu1 extends JFrame implements ActionListener {
// Menu Bar declaration
static JMenuBar xb;
// JMenu declaration
static JMenu x, q1;
// Menu items
static JMenuItem x1, x2, x3, y1,y2;
// frame creation declaration
static JFrame y;
// label declaration
static JLabel l;
public static void main ()
{
// create an object of the class menu1
menu1 m = new menu1();
// create a frame
z = new JFrame ("Menu demo");
// creating a label
z1= new JLabel ("no task ");
// creating a menu bar
xb = new JMenuBar ();// creating a menu
x = new JMenu("Menu");
q1 = new JMenu("submenu");
// creating menu items
x1 = new JMenuItem("MenuItem1");
x2 = new JMenuItem("MenuItem2");
x3 = new JMenuItem("MenuItem3");
y1 = new JMenuItem("SubMenuItem1");
y2 = new JMenuItem("SubMenuItem2");
// adding ActionListener to the menu Items used
x1.addActionListener(m);
x2. addActionListener(m);
x3. addActionListener(m);
y1.addActionListener(m);
y2.addActionListener(m);
// adding menu items to menu lined up
x.add (x1);
x.add (x2);
x.add (x3);
q1.add(y1);
q1.add(y2);
// adding submenu in the list
x.add(q1);
// adding menu to menu bar
xb.add(x);
// adding menu bar to frame
setJMenuBar(xb);
y.add(l);
setSize (500, 500);
y.setVisible(true);
}
public void action Performed (ActionEvent d)
{
String s1 = d. getActionCommand ();
// set the label to the menu Item that is selected
l.setText(s1 + " used");
}
}
Output:
Conclusion
In this article, we have presented all the necessary JMenu as well as other operators used in Java Swing coding. We have learned about the assorted collection of constructors used in JMenu which supports to consolidate objects into articulations. A large portion of the models we have seen so far have included just basic nuclear information.
Recommended Articles
This is a guide to JMenu. Here we discuss the introduction, constructors, ordinarily utilized techniques and examples with proper code and output. You may also have a look at the following articles to learn more –