+92 332 4229 857 99ProjectIdeas@Gmail.com

How to insert JMenuBar in JFrame/Window (JAVA)


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:

JMenuBar menuBar = new JMenuBar();

Now, creating “File” menu and its submenu (“New”,”Open”,”Save”,”Exit”):

JMenu fMenu = new JMenu("File");

Setting short key Alt + F to open File menu:

fMenu.setMnemonic(KeyEvent.VK_F); // short key ALT + F
             
Now, here creating submenus with their names and also inserting images on each menu item and then setting text position to right:

JMenuItem newItem = new JMenuItem("New", new ImageIcon("Shield 16x16.png"));
newItem.setHorizontalTextPosition(JMenuItem.RIGHT);
           
JMenuItem open = new JMenuItem("Open", new ImageIcon("Wrench.png"));
open.setHorizontalTextPosition(JMenuItem.RIGHT);
                       
JMenuItem saveAs = new JMenuItem("Save", new ImageIcon("Save.png"));
saveAs.setHorizontalTextPosition(JMenuItem.RIGHT);
                       
JMenuItem exit = new JMenuItem("Exit", new ImageIcon("Delete.png"));
exit.setHorizontalTextPosition(JMenuItem.RIGHT);

Now, adding all submenus under its parent menu File:

fMenu.add(newItem);
fMenu.add(open);
fMenu.add(saveAs);           
fMenu.add(exit);

In the same way creating Edit, Tools and Help menus and their submenus.

JMenu eMenu = new JMenu("Edit");
eMenu.setMnemonic(KeyEvent.VK_E); // short key ALT + E
           
JMenuItem undo = new JMenuItem("Undo", new ImageIcon("Undo2.png"));
undo.setHorizontalTextPosition(JMenuItem.RIGHT);
           
JMenuItem redo = new JMenuItem("Redo", new ImageIcon("Redo2.png"));
redo.setHorizontalTextPosition(JMenuItem.RIGHT);
           
JMenuItem copy = new JMenuItem("Copy", new ImageIcon("Copy.png"));
copy.setHorizontalTextPosition(JMenuItem.RIGHT);
           
JMenuItem paste = new JMenuItem("Paste", new ImageIcon("Paste.png"));
paste.setHorizontalTextPosition(JMenuItem.RIGHT);
           
Now, adding all submenus under its parent menu Edit:

eMenu.add(undo);
eMenu.add(redo);
eMenu.add(copy);
eMenu.add(paste);
           
Tools menu:

JMenu tMenu = new JMenu("Tools");
tMenu.setMnemonic(KeyEvent.VK_T); // short key ALT + T
           
JMenuItem bMenu = new JMenuItem("Create Javadoc", new ImageIcon("Bookmark.png"));
bMenu.setHorizontalTextPosition(JMenuItem.RIGHT);
                       
JMenuItem favourite = new JMenuItem("Favourites", new ImageIcon("Fav.png"));
           
JMenuItem history = new JMenuItem("History", new ImageIcon("History.png"));
history.setHorizontalTextPosition(JMenuItem.RIGHT);
           
Now, adding all submenus under its parent menu Tools:

tMenu.add(bMenu);
tMenu.add(favourite);
tMenu.add(history);
  

Help menu:
     
JMenu hMenu = new JMenu("Help");
hMenu.setMnemonic(KeyEvent.VK_H); // short key ALT + H
           
JMenuItem about = new JMenuItem("About", new ImageIcon("About.png"));
about.setHorizontalTextPosition(JMenuItem.RIGHT);
           
Now, adding all submenus under its parent menu Help:

hMenu.add(about);

Now, adding each File, Edit, Tools and Help menus to main JMenuBar (menuBar):

menuBar.add(fMenu);
menuBar.add(eMenu);          
menuBar.add(tMenu);
menuBar.add(hMenu);     

Displaying/setting JMenuBar on your JFrame/Window:

myFrame.setJMenuBar(menuBar);

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:

newItem.addActionListener(new ActionListener() {
                             
   public void actionPerformed(ActionEvent ae) {
                       
      JOptionPane.showMessageDialog(null, "You Pressed New Item Sub Menu");

             // Do Your Work Here
                       
   }
});


0 comments: