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:
Constructor | Description |
---|---|
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:
Constructor | Description |
---|---|
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:
Constructor | Description |
---|---|
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.



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;
Menubar(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(400,400);
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");
loadItem.addActionListener(this);
saveItem.addActionListener(this);
exitItem.addActionListener(this);
loadItem.setIcon(fileIcon);
saveItem.setIcon(saveIcon);
exitItem.setIcon(exitIcon);
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
fileMenu.add(loadItem);
fileMenu.add(saveItem);
fileMenu.add(exitItem);
jm_bar.add(fileMenu);
jm_bar.add(editMenu);
jm_bar.add(helpMenu);
this.setJMenuBar(jm_bar);
this.setVisible(true);
}
@Override
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) {
System.exit(0);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Menubar();
}
}
Comments (No)