JMenuBar, JMenu and JMenuItems are a part of Java Swing package. JMenuBar is an implementation of menu bar . the JMenuBar contains one or more JMenu objects, when the JMenu objects are selected they display a popup showing one or more JMenuItems .
JMenu basically represents a menu . It contains several JMenuItem Object . It may also contain JMenu Objects (or submenu).
- JMenuBar() : Creates a new MenuBar.
- JMenu() : Creates a new Menu with no text.
- JMenu(String name) : Creates a new Menu with a specified name.
- JMenu(String name, boolean b) : Creates a new Menu with a specified name and boolean
value specifies it as a tear-off menu or not. A tear-off menu can be opened and dragged away from its parent menu bar or menu.
Commonly used methods:
- add(JMenu c) : Adds menu to the menu bar. Adds JMenu object to the Menu bar.
- add(Component c) : Add component to the end of JMenu
- add(Component c, int index) : Add component to the specified index of JMenu
- add(JMenuItem menuItem) : Adds menu item to the end of the menu.
- add(String s) : Creates a menu item with specified string and appends it to the end of menu.
- getItem(int index) : Returns the specified menuitem at the given index
The following programs will illustrate the use of JMenuBar
1. Program to make a MenuBar and add menu items to it
2. Program to add a menubar and add menuitems, submenu items and also add ActionListener to menu items
Note : The following program might not run in an online compiler, please use an offline IDE.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Java Swing | Translucent and shaped Window in Java
- Java Swing | JFileChooser
- Java Swing | JSpinner
- Java Swing | JSlider
- Java Swing | MatteBorder
- JRadioButton | Java Swing
- Java Swing | JPasswordField
- Java Swing | JTable
- Java Swing | JProgressBar
- Java Swing | JTextField
- Java Swing | JToolBar
- Java Swing | ToolTip
- Java Swing | Look and Feel
- JLabel | Java Swing
- Java Swing | JTextArea
- Java Swing | JPopupMenu
- Java Swing | JComboBox with examples
- Java Swing | JSplitPane with Examples
- Java Swing | GroupLayout Class
- Java Swing | BevelBorder and SoftBevelBorder
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.