Create Java MenuBar and Menus

Create Java MenuBar:

We can use the Eclipse IDE for this example. If you do not know about it then follow this link- How to Install Eclipse For Java and create a program in it.

A MenuBar represents a list of menus that can be added to the top of a top-level window. Each Menu is associated with a drop-down list of MenuItems. The concept of MenuBar can be implemented using three Java classes i.e. JMenuBar, JMenu, and JMenuItem. A MenuBar is represented by the object of class JMenuBar in which only the default constructor is defined. A MenuBar may consist of one or more Menus represented by the objects of the class JMenu. A Menu contains a list of MenuItems represented by the objects of the class JMenuItem.

JMenu Class Constructors:

JMenu( )Creates an empty Menu.
JMenu(String optionName)Creates a Menu with the name of the Menu specified by optionName.
JMenu(String optionName, boolean removable)Removable may have one of two values: true or false. If it is true then the menu can float freely in the application window, otherwise, it remains attached to the MenuBar.

JMenuItem Class Constructors:

JMenuItem( )Creates a MenuItem with no name and no Menu shortcut.
JMenuItem(String itemName)itemName specifies the name of the MenuItem.
JMenuItem(String itemName, Icon icon)icon specifies the icon associated with the MenuItem.

A checkable menu item can also be created by using a subclass of JMenuItem called JCheckboxMenuItem.

JCheckboxMenuItem Class Constructors:

JCheckboxMenuItem( )Creates an unchecked MenuItem with no name.
JCheckboxMenuItem(String itemName)Creates an unchecked MenuItem with the name of the MenuItem specified by itemName.
JCheckboxMenuItem(String itemName, boolean checkable)checkable can either be true or false. If it is true then the MenuItem is initially checked, otherwise, it is unchecked.

Sample Code to Create Java MenuBar and Menus in Java:

Download images from here and paste them into the java project folder.

menubar java code
jmenubar code
java menubar code


java menubar output
import java.awt.*;
import java.awt.FlowLayout;
import java.awt.event.*;

import javax.swing.*;

public class Menubar extends JFrame implements ActionListener {
	JMenuBar jm_bar;
	JMenu fileMenu;
	JMenu editMenu;
	JMenu helpMenu;
	JMenuItem loadItem;
	JMenuItem saveItem;
	JMenuItem exitItem;
	ImageIcon fileIcon;
	ImageIcon saveIcon;
	ImageIcon exitIcon;
		this.setLayout(new FlowLayout());
		fileIcon = new ImageIcon("file.jpg");
		saveIcon = new ImageIcon("save.png");
		exitIcon = new ImageIcon("exit.png");
		jm_bar = new JMenuBar();
		fileMenu = new JMenu("File");
		editMenu = new JMenu("Edit");
		helpMenu = new JMenu("Help");
		loadItem = new JMenuItem("Load");
		saveItem = new JMenuItem("Save");
		exitItem = new JMenuItem("Exit");
		fileMenu.setMnemonic(KeyEvent.VK_F); // Alt + f for file
		editMenu.setMnemonic(KeyEvent.VK_E); // Alt + e for edit
		helpMenu.setMnemonic(KeyEvent.VK_H); // Alt + h for help
		loadItem.setMnemonic(KeyEvent.VK_L); // L for load
		saveItem.setMnemonic(KeyEvent.VK_S); // S for save
		exitItem.setMnemonic(KeyEvent.VK_E); // E for exit
	public void actionPerformed(ActionEvent e) {
        if(e.getSource()==loadItem) {
			System.out.println("*tic tac* you loaded a file");
        if(e.getSource()==saveItem) {
			System.out.println("*tic tac* you saved a file");
        if(e.getSource()==exitItem) {
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	  new Menubar();


Tutorial MySQL
Tutorial Python
Internet and Its Uses
World Wide Web (WWW)
Malicious Software (Malware)
Database Management System (DBMS)
Chr and Ord Function in Python
List Comprehension in Python
Implement Queue in Python
Java (programming language)– Wikipedia

Comments (No)

Leave a Reply