Creating a menu bar in Swing is a fundamental aspect of developing Java applications with a graphical user interface (GUI). As a Swing supplier, I’ve witnessed firsthand the importance of well – designed menu bars in enhancing user experience and application functionality. In this blog, I’ll guide you through the process of creating a menu bar in Swing, from the basics to more advanced features. Swing

Understanding the Basics of Swing Menu Bars
Swing is a set of GUI components for Java, and it provides a rich set of tools for creating menu bars. A menu bar is typically located at the top of an application window and contains a series of menus. Each menu can have multiple menu items, and some menu items can have sub – menus.
To start creating a menu bar in Swing, you first need to import the necessary Java Swing packages. Here is a simple code snippet to get you started:
import javax.swing.*;
import java.awt.*;
public class SimpleMenuBarExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Simple Menu Bar Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem openItem = new JMenuItem("Open");
JMenuItem saveItem = new JMenuItem("Save");
fileMenu.add(openItem);
fileMenu.add(saveItem);
menuBar.add(fileMenu);
frame.setJMenuBar(menuBar);
frame.setVisible(true);
}
}
In this code, we first create a JFrame, which is the main window of our application. Then we create a JMenuBar object. Next, we create a JMenu called "File" and two JMenuItem objects: "Open" and "Save". We add these menu items to the "File" menu and then add the "File" menu to the menu bar. Finally, we set the menu bar for the frame and make the frame visible.
Adding More Menus and Menu Items
You can easily add more menus and menu items to your menu bar. For example, let’s add an "Edit" menu with some common edit – related menu items:
import javax.swing.*;
import java.awt.*;
public class ExtendedMenuBarExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Extended Menu Bar Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
JMenuBar menuBar = new JMenuBar();
// File menu
JMenu fileMenu = new JMenu("File");
JMenuItem openItem = new JMenuItem("Open");
JMenuItem saveItem = new JMenuItem("Save");
fileMenu.add(openItem);
fileMenu.add(saveItem);
menuBar.add(fileMenu);
// Edit menu
JMenu editMenu = new JMenu("Edit");
JMenuItem cutItem = new JMenuItem("Cut");
JMenuItem copyItem = new JMenuItem("Copy");
JMenuItem pasteItem = new JMenuItem("Paste");
editMenu.add(cutItem);
editMenu.add(copyItem);
editMenu.add(pasteItem);
menuBar.add(editMenu);
frame.setJMenuBar(menuBar);
frame.setVisible(true);
}
}
In this extended example, we create a new JMenu called "Edit" and add three menu items: "Cut", "Copy", and "Paste". We then add the "Edit" menu to the menu bar.
Using Sub – Menus
Sub – menus are useful when you want to group related menu items. For instance, in the "File" menu, we can add a "Recent Files" sub – menu.
import javax.swing.*;
import java.awt.*;
public class SubMenuExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Sub - Menu Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
JMenuBar menuBar = new JMenuBar();
// File menu
JMenu fileMenu = new JMenu("File");
JMenuItem openItem = new JMenuItem("Open");
JMenuItem saveItem = new JMenuItem("Save");
fileMenu.add(openItem);
fileMenu.add(saveItem);
// Recent files sub - menu
JMenu recentFilesMenu = new JMenu("Recent Files");
JMenuItem recentFile1 = new JMenuItem("File 1");
JMenuItem recentFile2 = new JMenuItem("File 2");
recentFilesMenu.add(recentFile1);
recentFilesMenu.add(recentFile2);
fileMenu.add(recentFilesMenu);
menuBar.add(fileMenu);
frame.setJMenuBar(menuBar);
frame.setVisible(true);
}
}
In this code, we create a new JMenu called "Recent Files" and add two menu items to it. Then we add this sub – menu to the "File" menu.
Adding Action Listeners to Menu Items
Menu items are not very useful if they don’t perform any actions. You can add action listeners to menu items to handle user clicks.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ActionListenerExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Action Listener Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
JMenuBar menuBar = new JMenuBar();
// File menu
JMenu fileMenu = new JMenu("File");
JMenuItem openItem = new JMenuItem("Open");
openItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "Open action triggered");
}
});
JMenuItem saveItem = new JMenuItem("Save");
saveItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "Save action triggered");
}
});
fileMenu.add(openItem);
fileMenu.add(saveItem);
menuBar.add(fileMenu);
frame.setJMenuBar(menuBar);
frame.setVisible(true);
}
}
In this example, we add action listeners to the "Open" and "Save" menu items. When a user clicks on these menu items, a message dialog will be displayed.
Customizing the Appearance of Menu Bars
You can customize the appearance of menu bars, menus, and menu items to match the look and feel of your application. For example, you can change the font, color, and background color.
import javax.swing.*;
import java.awt.*;
public class AppearanceCustomizationExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Appearance Customization Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
JMenuBar menuBar = new JMenuBar();
menuBar.setBackground(Color.LIGHT_GRAY);
menuBar.setForeground(Color.BLUE);
JMenu fileMenu = new JMenu("File");
fileMenu.setFont(new Font("Arial", Font.BOLD, 14));
fileMenu.setForeground(Color.RED);
JMenuItem openItem = new JMenuItem("Open");
openItem.setBackground(Color.YELLOW);
openItem.setForeground(Color.GREEN);
fileMenu.add(openItem);
menuBar.add(fileMenu);
frame.setJMenuBar(menuBar);
frame.setVisible(true);
}
}
In this code, we change the background and foreground colors of the menu bar, the font and foreground color of the menu, and the background and foreground colors of a menu item.
Advanced Features: Keyboard Shortcuts and Icons
You can add keyboard shortcuts to menu items to make them more accessible. You can also add icons to menu items to make them more visually appealing.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class AdvancedFeaturesExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Advanced Features Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
ImageIcon openIcon = new ImageIcon("open.png");
JMenuItem openItem = new JMenuItem("Open", openIcon);
openItem.setAccelerator(KeyStroke.getKeyStroke('O', Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
openItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "Open action triggered");
}
});
fileMenu.add(openItem);
menuBar.add(fileMenu);
frame.setJMenuBar(menuBar);
frame.setVisible(true);
}
}

In this example, we add an icon to the "Open" menu item and set a keyboard shortcut (Ctrl + O on Windows and Command + O on Mac).
Conclusion
Swing Chains Creating a menu bar in Swing is a straightforward process, but it offers a lot of flexibility and features. Whether you are building a simple application or a complex one, a well – designed menu bar can greatly enhance the user experience. As a Swing supplier, we are here to provide you with high – quality Swing components and support. If you are interested in purchasing Swing components for your projects or need more in – depth consultation on creating menu bars or other Swing – related features, please feel free to contact us for procurement and negotiation.
References
- "Java Swing Tutorial" by Oracle
- "Effective Java" by Joshua Bloch
Pujiang Shenli Chain Co., Ltd.
We’re well-known as one of the most experienced swing suppliers in China, featured by quality products and low price. Please feel free to buy discount swing made in China here from our factory. Contact us for more details.
Address: No. 18, Zaifeng Road, Pujiang County, Zhejiang Province
E-mail: Chen@shenlichain.com
WebSite: https://www.chainshenli.com/