AWT MenuBar
The MenuBar class helps manage the menu bar of a particular frame. The menu bar handles the menu items that are a part of it. It helps set keyboard shortcuts for easy access and passes them along to its child menus.
Signature:
public class MenuBar
extends MenuComponent
implements MenuContainer, Accessible
You can also try this code with Online Java Compiler
Run Code
Constructors
AWT MenuBar has one constructor.
MenuBar() - This creates a new Menu bar.
This class inherits methods from the java.awt.MenuComponent and java.lang.Object.
Functions
You can also read about the Multiple Inheritance in Java.
AWT MenuItem
All the items inside a menu belong to the MenuItem class. Each MenuItem is associated with an ActionEvent that is triggered when the MenuItem is clicked. The processEvent method examines the event and passes it to the processActionEvent function, which redirects the event to any ActionListener objects.
The More Examples MenuItem is a subMenu. The Action Event behaviour is overridden and does not send any event to the frame until any one of its sub-MenuItems is selected.
Signature:
public class MenuItem
extends MenuComponent
implements Accessible
You can also try this code with Online Java Compiler
Run Code
Constructors
AWT MenuItem has three constructors.
Functions
Example
import java.awt.*;
import java.awt.event.*;
public class MenuSample
{
private Frame mainFrame;
private Label headerLabel;
private Label statusLabel;
private Panel controlPanel;
public MenuSample()
{
MenuGUI();
}
public static void main(String[] args)
{
MenuSample ms = new MenuSample();
ms.showMenuDemo();
}
private void MenuGUI()
{
//create a frame object
mainFrame = new Frame("Java AWTMenu");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent)
{
System.exit(0);
}
});
headerLabel = new Label();
headerLabel.setAlignment(Label.CENTER);
statusLabel = new Label();
statusLabel.setAlignment(Label.CENTER);
statusLabel.setSize(350,100);
controlPanel = new Panel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showMenuDemo()
{
//create a menu bar
final MenuBar menuBar = new MenuBar();
//create menus
Menu fileMenu = new Menu("File");
final Menu aboutMenu = new Menu("About");
//create menu items
MenuItem newMenuItem =
new MenuItem("New",new MenuShortcut(KeyEvent.VK_N));
newMenuItem.setActionCommand("New");
MenuItem openMenuItem = new MenuItem("Open");
openMenuItem.setActionCommand("Open");
MenuItem saveMenuItem = new MenuItem("Save");
saveMenuItem.setActionCommand("Save");
MenuItem exitMenuItem = new MenuItem("Exit");
exitMenuItem.setActionCommand("Exit");
MenuItemListener menuItemListener = new MenuItemListener();
//adding ActionListener
newMenuItem.addActionListener(menuItemListener);
openMenuItem.addActionListener(menuItemListener);
saveMenuItem.addActionListener(menuItemListener);
exitMenuItem.addActionListener(menuItemListener);
//add menu items to menus
fileMenu.add(newMenuItem);
fileMenu.add(openMenuItem);
fileMenu.add(saveMenuItem);
fileMenu.addSeparator();
fileMenu.add(exitMenuItem);
//add menus to menubar
menuBar.add(fileMenu);
menuBar.add(aboutMenu);
//add menubar to the frame
mainFrame.setMenuBar(menuBar);
mainFrame.setVisible(true);
}
//Setting ActionEvent
class MenuItemListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
statusLabel.setText(e.getActionCommand()
+ " MenuItem clicked.");
}
}
}
You can also try this code with Online Java Compiler
Run Code
Try and compile it by yourself on java online compiler.
The above code creates a MenuBar with two Menu objects - File and About. The File object contains four MenuItems - New, Open, Save and Exit. There is a separator between Save and Exit. On clicking any one of the MenuItems, It displays a Menu Item clicked message. This is the Action Event set to the menu items. The ActionListener listens for the clicking action at the MenuItem and triggers the Action event.
Frequently Asked Questions
What is a Listener?
A listener is an object that gets notified when an event occurs. For this to happen, there are two conditions to be met. First, it must have been registered with a source to receive signals when a specific event occurs. Second, it must implement the necessary methods to receive and process the signals.
What is the difference between a MenuItem and CheckBoxMenuItem?
The MenuItem class is a subclass of MenuComponent, and the CheckboxMenuItem class is a subclass of the MenuItem class. MenuItem only specifies the MenuItem with a name, while CheckBoxMenuItem specifies a MenuItem that can be checked or unchecked.
Conclusion
This article has extensively discussed the AWT Menu, MenuBar and MenuItem classes. We explored the constructors and functions associated with Menu, MenuBar and MenuItem. Lastly, we learnt how to use the three classes to build a MenuBar with menus and menu items in a Java Frame.
Feeling curious? Coding Ninjas has you covered. Check out our articles on AWT List, AWT Panel, AWT Label, Difference between AWT and Swing and JavaFX. Follow our Guided path for Programming in Java here.
Explore our Library on Coding Ninjas Studio to gain knowledge on Data Structures and Algorithms, Machine Learning, Deep Learning, Cloud Computing and many more! Test your coding skills by solving our test series and participating in the contests hosted on Coding Ninjas Studio!
Looking for questions from tech giants like Amazon, Microsoft, Uber, etc.? Look at the problems, interview experiences, and interview bundle for placement preparations.
Upvote our blogs if you find them insightful and engaging! Happy Coding!