Table of contents
1.
Introduction
2.
Class Declaration
3.
Syntax of JMenu
4.
Class Constructors
5.
Class Methods
6.
Example 1
7.
Example 2
8.
Frequently Asked Questions
8.1.
What is JMenu in Java?
8.2.
What is the difference between applet and Swing?
8.3.
What are the benefits of utilizing Jmenubars in an application?
8.4.
Which Java version has Swing?
8.5.
Which property is used to sort ComboBox items?
9.
Conclusion
Last Updated: Mar 27, 2024

JMenu

Author Komal Shaw
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

JMenu, JMenuBar, and JMenuItems are Java Swing components. JMenuBar is a menu bar implementation. At least one JMenu object is present in the JMenuBar. When these items are selected, a popup appears that shows at least one JMenuItems. It essentially communicates with a menu. It has a couple of JMenuItem Objects in it. It could also include JMenu Objects (or submenu). The Menu class corresponds to the drawdown menu component of a menu bar. The JMenuItem class represents an actual menu item. JMenuItem or one of its subclasses should be used for anything in a menu.

Class Declaration

JMenuBar class declaration

public class JMenuBar extends JComponent implements MenuElement, Accessible
You can also try this code with Online Java Compiler
Run Code

 

JMenu class declaration

public class JMenu extends JMenuItem implements MenuElement, Accessible
You can also try this code with Online Java Compiler
Run Code

 

JMenuItem class declaration

public class JMenuItem extends AbstractButton implements Accessible, MenuElement
You can also try this code with Online Java Compiler
Run Code

Syntax of JMenu

JMenuBar in Java Swing

JMenuBar <JMenuBar_name> = new JMenuBar();
You can also try this code with Online Java Compiler
Run Code

 

JMenuBar at the highest point of the holder

setJMenuBar(<JMenuBar_name>);
You can also try this code with Online Java Compiler
Run Code

 

Making a different menu and adding the menu to the JMenuBar

JMenu <JMenu_name> = new JMenu("menu_name");
<JMenuBar_name>. add(<JMenu_name>);
You can also try this code with Online Java Compiler
Run Code

 

JmenuItem in Java Swing

JMenuItem <JMenuItem_name> = new JMenuItem("menu_name ");
You can also try this code with Online Java Compiler
Run Code

Class Constructors

CONSTRUCTOR

DESCRIPTION

JMenu() It creates a new JMenu that has no text.
JMenu(Action a) It creates a menu with characteristics based on the Action provided.
JMenu(String s) It creates a new JMenu with the text specified as its text.
JMenu(String s, boolean b) It creates a new JMenu using the provided string as the text and the option to make it a tear-off menu or not.

Class Methods

Here is a list of methods in the Swing JMenu control class:

  1. javax.swing.JAbstractButton
  2. javax.swing.JComponent
  3. java.awt.Container
  4. java.awt.Component
  5. java.lang.Object

Example 1

Here is an example for better understanding: we create a JMenu in JMenubar with the text "Menu Label" containing 4 JMenuItems.

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class JavaMenuItemExample
{
	public static void main(String[] argv) throws Exception
	{
		JMenuBar menuBar = new JMenuBar();
		JMenu menu = new JMenu("Menu Label");
		menuBar.add(menu);
		
		JMenuItem item1 = new JMenuItem("Item Label1");
		JMenuItem item2 = new JMenuItem("Item Label2");
		JMenuItem item3 = new JMenuItem("Item Label3");
		JMenuItem item4 = new JMenuItem("Item Label4");
		
		menu.add(item1);
		menu.add(item2);
		menu.add(item3);
		menu.add(item4);

		JFrame frame = new JFrame();
		frame.setJMenuBar(menuBar);
		frame.setVisible(true);
	}
}

 

OUTPUT

Example 2

Here is another example of JMenu with the text "File" containing 3 JMenuItems: Open, Save, and Quit.

import java.awt.event.*;
import javax.swing.*;
 
public class JMenuExample extends JFrame implements ActionListener {
 
    public static void main(String[] s) {
        new JMenuExample();
    }
 
    public JMenuExample() {
 
        super("JMenu Example");
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
 
        /*  Name the JMenu & Add Items  */

        JMenu menu = new JMenu("File");
        menu.add(makeMenuItem("Open"));
        menu.add(makeMenuItem("Save"));
        menu.add(makeMenuItem("Quit"));
 
        /*  Add JMenu bar  */

        JMenuBar menuBar = new JMenuBar();
        menuBar.add(menu);
        setJMenuBar(menuBar);
        setSize(300, 300);
        setLocation(200, 200);
        setVisible(true);
    }
 
    public void actionPerformed(ActionEvent e) {
 
        /*  Menu item actions  */

        String command = e.getActionCommand();
 
        if (command.equals("Quit")) {
            System.exit(0);
        } else if (command.equals("Open")) {
        
            /*  Open menu item action  */
            
            System.out.println("Open menu item clicked");
        } else if (command.equals("Save")) {
        
            /*  Save menu item action  */
            
            System.out.println("Save menu item clicked");
        }
    }
 
    private JMenuItem makeMenuItem(String name) {
        JMenuItem m = new JMenuItem(name);
        m.addActionListener(this);
        return m;
    }
}


OUTPUT

Frequently Asked Questions

What is JMenu in Java?

JMenuBar is a menu bar implementation.

What is the difference between applet and Swing?

Applets are small bits of functionality that operate in a web browser and can be downloaded on demand. Swing is a set of user interface components, such as text boxes and windows, that a developer can create for usage on the desktop.

What are the benefits of utilizing Jmenubars in an application?

JMenuBar is a class that displays a menubar on a window or frame. It could have multiple menus. The JMenu class's object is a pull-down menu component that appears in the menu bar.

Which Java version has Swing?

Since Java 1.2, Swing has been included as part of the Java Standard Edition.

Which property is used to sort ComboBox items?

The Sorted Property allows you to sort the elements in the ComboBox.

Conclusion

In this article, we have extensively discussed JMenu In Java, also learned about the commonly used constructors and methods, and saw some examples.

We hope this blog has helped you enhance your knowledge regarding JMenu In Java.

If you want to learn more, check out our articles on JProgressBarGraphics in java swingSwing Components in java, and JComboBox In Java.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc.

Enroll in our courses and refer to the mock test and problems available.

Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Happy Coding!

Live masterclass