- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a JMenuBar Component in Java?
To create a JMenuBar component, use the JMenuBar class −
JMenuBar menuBar = new JMenuBar();
Now, create menus inside the MenuBar −
JMenu fileMenu = new JMenu("File");
Add the above menu to the MenuBar −
menuBar.add(fileMenu);
The following is an example to create a JMenuBar Component in Java −
Example
package my; import java.awt.event.KeyEvent; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; public class SwingDemo { public static void main(final String args[]) { JFrame frame = new JFrame("MenuBar Demo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JMenuBar menuBar = new JMenuBar(); JMenu fileMenu = new JMenu("File"); fileMenu.setMnemonic(KeyEvent.VK_F); menuBar.add(fileMenu); JMenuItem menuItem1 = new JMenuItem("New", KeyEvent.VK_N); fileMenu.add(menuItem1); JMenuItem menuItem2 = new JMenuItem("Open File", KeyEvent.VK_O); fileMenu.add(menuItem2); JMenu editMenu = new JMenu("Edit"); editMenu.setMnemonic(KeyEvent.VK_E); menuBar.add(editMenu); frame.setJMenuBar(menuBar); frame.setSize(550, 350); frame.setVisible(true); } }
Output
- Related Articles
- How to create Titled Border for a component in Java?
- How to create a compound border for a component in Java?
- How to create two borders for a single component in Java?
- How to create etched border for a component using BorderFactory class in Java?
- How to create an invisible fixed width component between two components in Java?
- How to create an invisible fixed height component between two components in Java?
- How to add a tooltip to a component in Java?
- Java program to insert a component into a JTextPane component
- How to set vertical alignment for a component in Java?
- How to find a node in a JTree Component with Java?
- How to disallow resizing component with GridBagLayout in Java
- How to limit the values in a Number JSpinner Component with Java?
- How to display a large component within a smaller display area in Java?
- Set a component and place it next to the previously added component with GridBagLayout in Java
- How to center align component using BoxLayout with Java?

Advertisements