How to insert JMenuBar in JFrame/Window:
The following code shows how you can insert JMenuBar in your application, creating submenus and putting images on them and also how to handle events on them.
Creating JMenuBar first like this:
Now, creating “File” menu and its submenu (“New”,”Open”,”Save”,”Exit”):
Setting short key Alt + F to open File menu:
Now, here creating submenus with their names and also inserting images on each menu item and then setting text position to right:
Now, adding all submenus under its parent menu File:
In the same way creating Edit, Tools and Help menus and their submenus.
Now, adding all submenus under its parent menu Edit:
Now, adding all submenus under its parent menu Tools:
Now, adding all submenus under its parent menu Help:
Now, adding each File, Edit, Tools and Help menus to main JMenuBar (menuBar):
Displaying/setting JMenuBar on your JFrame/Window:
Handling events on menu item using Anonymous Inner Classes; here I am generating action event (if user click on that item then this work is to be done) on “New” submenu under “File” menu, like this: