Swing Menu Bar

Swing is simple yet elegant way to create desktop based friendly user interface(UI). The desktop UI contain lot’s of different details. But today, we will create a simple menu bar that will just look like menu bar, we see daily while using different apps. The menu bar are important part of any program. It’s a way to navigate through the program, perform different actions, software customization and ease for usage. Menu bar contains lots of program features, options and distinct actions like ‘Save’, ‘New’ etc.
Note : Menu Bar by default always appear at the top of the window frame.


        KeyStroke keyNew = KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK);
        newMenuItem.setAccelerator(keyNew);

Assign key ‘CTRL + N’ series to Menu Item.


     newMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // CODE TO BE EXECUTED WHEN NEW MENU IS CLICKED
            }
        });

Add Action listener to the menu item. Code to be executed or method to be called will be placed inside here, which only will be triggered when user will click the menu item.


            menuFile.add(newMenuItem);
            ...
            menuBarTop.add(menuFile);

Menu item added to menu and that menu is added to the menu bar.

Below is the full code on how to create a simple menu bar with menu items for swing based window frame.

package swing.ui;

import javax.swing.BorderFactory.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;
import javax.swing.KeyStroke;

/**
 *
 * @author prajin
 */
public class MenuBarExample {

    JFrame frame;
    Font font1;

    public MenuBarExample() {
        initUIComponents();
    }

    public void initUIComponents() {
        frame = new JFrame("| MENU BAR EXAMPLE |");
        font1 = new Font("Batang", 0, 14);
        JMenuItem newMenuItem = new JMenuItem(" New  ");
        newMenuItem.setFont(font1);
        //--- mnemonic settings -----------
        newMenuItem.setMnemonic(KeyEvent.VK_N);
        KeyStroke keyNew = KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK);
        newMenuItem.setAccelerator(keyNew);
        newMenuItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                // CODE TO BE EXECUTED WHEN NEW MENU IS CLICKED
            }
        });

        JMenuItem exitMenuItem = new JMenuItem(" Exit  ");
        exitMenuItem.setFont(font1);
        exitMenuItem.setForeground(Color.RED);
        exitMenuItem.setMnemonic(KeyEvent.VK_Q);
        exitMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, InputEvent.ALT_MASK));

        JSeparator separator1 = new JSeparator();

        JMenuItem helpContentMenuItem = new JMenuItem(" Help Contents  ");
        helpContentMenuItem.setFont(font1);
        helpContentMenuItem.setMnemonic(KeyEvent.VK_F1);
        helpContentMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, InputEvent.SHIFT_MASK));

        JMenuBar menuBarTop = new JMenuBar();
        JMenu menuFile = new JMenu(" File ");
        JMenu menuHelp = new JMenu(" Help ");

        menuFile.setFont(font1);
        menuHelp.setFont(font1);

        menuFile.add(newMenuItem);
        menuFile.add(separator1);
        menuFile.add(exitMenuItem);
        menuHelp.add(helpContentMenuItem);

        menuBarTop.add(menuFile);
        menuBarTop.add(menuHelp);

        frame.setJMenuBar(menuBarTop);
        frame.pack();
        frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
        frame.setResizable(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main(String args[]) {
        new MenuBarExample();
    }
}

The output should look like this :

Swing based Menu Bar

Leave a comment

Your email address will not be published. Required fields are marked *

 

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