Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

JMenuBar, JMenu, JMenuItem

1. Introduction – JMenuBar, JMenu, JMenuItem

The components JMenuBar, JMenu and JMenuItem all together form the Java Swing Menu based user interface. Java Swing Top-Level containers houses the JMenuBar. The JMenuBar control can hold one or more JMenu control. One or more JMenuItem can be present in a single JMenu. To better understand this relation, have a look at the below picture:

MenuBar, Menu, MenuItems - Relationship
MenuBar, Menu, MenuItems – Relationship

The above picture shows a JFrame with a title. It contains a menu bar with two menus called File and Edit. The menu-bar shows only the title of the menus. When the user clicks the menu title, Java Swing will open the popup menu. The File Popup Menu contains 4 menu items and Edit Popup Menu contains three menu items. Each MenuItem will produce an ActionEvent which one can handle to take required action. For Example, when the user clicks File->New, the action event handler will have the code to open a new file.

Basic of JMenu, JMenuBar and JMenuItem

2. About Java Swing JMenu Example

The below screenshot shows the example we will create in the following sections:

Java Swing JMenu, JMenuItem - Example
Java Swing JMenu, JMenuItem – Example

Our example has a JMenuBar with two JMenu in it. The menus are File and Edit. File Menu has 4 MenuItems which include a JSeparator. This separator is separating Open, Save menu items with the Exit menu items. When you click Open MenuItem under the File Menu, a dialog will be displayed. This helps us to learn how to respond to the Menu Item click event. The Edit menu has 3 Menu Items. Now let us code this example.

About the JMenuItem Event Handling Example

3. Create JMenuBar

After setting the Size and position of the JFrame window, we create a JMenuBar at line number 13 and then set it to the JFrame. The method setJMenuBar accepts JMenuBar instance and set it to the JFrame.

4. Create JMenu

Next, we must create two Menus File and Edit. We use JMenu constructor to create them and the constructor takes a string which will be the Title for the Menu. After creating the JMenu, we add them to the menu-bar via its add method.

5. Create JMenuItem

Now we have Menu-Bar and Menu ready. We know that a JMenu can hold one or more menu items. In the below code, we create six JMenuItem objects and in the constructor; we pass the display string for the menu items. We also construct the JSeparator at line number 5 and separate the exit menu with the Open & Save Menu items. In code snippet 4a, we add Open, Save and Exit MenuItems along with Separator to the File menu. After this, we added the remaining JMenuItem objects to the Edit menu.

6. Handle JMenuItem Click

JMenuItem raises ActionEvent when we click it. One can handle this event via ActionListener just like how we handle the button click event. In the below code, we write an anonymous inner handler actionPerformed to handle the ActionEvent. Inside the handler, we just display a message box to state the event is handled. Here, we handled the event for File->Open MenuItem only. The procedure is the same for other Menu Items as well.

7. YouTube Demo – Code Implementation

Youtube Demo – Code Implementation

8. Code Reference



Categories: Swing

Tags: , , , , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.