Learn by easy steps

Menu 12

You can create menu by using Java code or by using Java swing menu controls. The way of creating Menu Bar and Menu items by using Java Swing Menus control has been shown in this tutorial. Follow the steps properly to create a menu with basic menu items.


Create a Java project named JavaMenu without any class.


Add a JFrame in the project to add the Menu.


Add a menu bar in the form by using Menu Bar control from Swing Menus.


Create the shortcut key to access the File menu by using nmemonic property. ‘F’ character is used here.


Right click on the File menu and select Menu Item to add new menu item for the File menu. Next, rename the menu item to New. Similar way, add Open and Save menu items.


Double click on the shortcut option of each menu item and add the shortcut key by using Accelerator window.


The menu will be look like the following images after adding the menu items with the shortcut keys.

File Menu:

Edit Menu:

Format Menu:


Add separator in the File Menu after adding three menu items.


File menu will be looked like the following image after adding separator and another menu item.


Set the following property of JFrame after selecting Code tab to display the window in the center of the screen.


Run the application. The window with the menu will be displayed in the center of the screen.