Java Abstract Window Toolkit (AWT) provides a comprehensive set of classes and methods to create graphical user interfaces. In this article, we will explore the MenuItem and Menu classes, which are essential for building menus in Java applications.
Java AWT MenuItem
MenuItem is a class that represents a simple labeled menu item within a menu. It can be added to a menu using the Menu.add(MenuItem mi) method.
Syntax of Class Declaration MenuItem:
public class MenuItem extends MenuComponent implements Accessible
Methods of the MenuItem Class
List of methods available in MenuItem with description.
|
Constructs a new MenuItem with the specified label.
|
Returns the label of the menu item.
|
Sets the label of the menu item to the specified string.
|
Adds an action listener to the menu item.
|
Java AWT Menu Class
The Java AWT Menu class represents a pop-up menu component in a graphical user interface (GUI) that can contain a collection of MenuItem objects. It provides a way to create and manage menus in Java AWT applications.
Syntax of Class Declaration of Menu
public class Menu extends MenuItem
Methods of the Menu Class
List of methods available in MenuItem with description
|
Constructs a new menu with the specified label.
|
Adds the specified menu item to the menu.
|
Adds a separator between menu items within the menu.
|
Returns the menu shortcut key associated with this menu.
|
Example usage of the MenuItem & Menu
Java
//Java class to implement AWT Menu
// and MenuItem
import java.awt.*;
import java.awt.event.*;
//Driver Class
public class MenuExample {
//Main Method
public static void main(String[] args) {
Frame frame = new Frame("Menu Example");
MenuBar menuBar = new MenuBar();
frame.setMenuBar(menuBar);
// Create a "File" menu
Menu fileMenu = new Menu("File");
MenuItem openItem = new MenuItem("Open");
MenuItem saveItem = new MenuItem("Save");
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.addSeparator();
// Create an "Exit" menu item with an action listener
MenuItem exitItem = new MenuItem("Exit");
exitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
//Added exit as item in MenuItem
fileMenu.add(exitItem);
menuBar.add(fileMenu);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
This code creates a simple window with a "File" menu that contains "Open," "Save," and "Exit" options. When you click "Exit," the application exits.
Output:

Output of the Example:

Conclusion
In this article, we explored the very necessary components of Java AWT's MenuItem and Menu classes.
- These classes are material for creating menus in graphical user interfaces.
- We discussed the phrase structure of class declarations,
- Registered the methods with their descriptions.
- Introduced the constructors for some classes.
By combining these classes and their joint methods, you can build interactive and user-friendly menus for your Java applications. The provided code example demonstrated how to make a simpleton fare bar, offer a practical starting point for development of more complex graphical interfaces in Java.
Similar Reads
JavaFX | MenuBar and Menu Menu is a popup menu that contains several menu items that are displayed when the user clicks a menu. The user can select a menu item after which the menu goes into a hidden state. MenuBar is usually placed at the top of the screen which contains several menus. JavaFX MenuBar is typically an impleme
5 min read
Java AWT PopupMenu In Java, AWT (Abstract Window Toolkit) provides a PopupMenu course that allows you to make pop-up menus in your graphical user port (GUI) applications. A PopupMenu is a setting menu that appears when you right-click on a component, such as a button or a venire. It provides a set of options or action
3 min read
Java JCheckBoxMenuItem Java JCheckBoxMenuItem is a GUI component in Swing that extends the JMenuItem class. It represents a Menu item with a checkbox that can be checked or unchecked. JCheckBoxMenuItem is often used in menu systems to allow users to toggle options. In this article, we are going to explore some constructor
4 min read
Java MouseListener in AWT The Abstract Window Toolkit (AWT) in Java provides a collection of user interface components and event-handling features for creating graphical user interfaces (GUIs). User interaction is an important component of GUI programming, and the MouseListener interface in AWT is an important tool for manag
3 min read
Java AWT List AWT or Abstract Window Toolkit is a set of various UI components with the List Component. We can display a scrollable list of items through which users can select one or multiple options. AWT List is embedded into the AWT library which is used to build a GUI to present a set of choices to the user.
9 min read
Java AWT | MenuShortcut Class MenuShortcut class is a part of JavaAWT. MenuShortcut class implements menu shortcuts which are implemented using virtual keycodes. The MenuShortcut class represents a keyboard accelerator for a MenuItem. Constructors of the class: MenuShortcut(int k): Creates a MenuShortcut object with specified ke
5 min read
Java AWT Panel In Java's Abstract Window Toolkit (AWT), the Panel class is a fundamental component for creating graphical user interfaces. It offers a straightforward way to organize and group various GUI elements. This article explores the Panel class in Java AWT, covering its essential aspects, methods, and cons
2 min read
Java AWT Toolkit The Abstract Window Toolkit (AWT) is a Java package that provides a platform-indepеndеnt sеt of tools for creating graphical usеr intеrfacеs (GUIs). AWT is part of thе Java Foundation Classеs (JFC), which also includes Swing for morе advancеd GUI dеvеlopmеnt. In this rеsponsе, I'll providе an ovеrvi
6 min read
JavaFX | MenuButton MenuButton is a part of the JavaFX library. The menuButton when pressed shows a context menu that displays a set of items and the user may select any item. It typically contains several menu items and the user may select at most one MenuItem at a time. Constructor of the MenuButton class are: MenuB
4 min read