• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Swing » MenuBar Example in Java Swing
Next →
← Prev

MenuBar Example in Java Swing

By Dinesh Thakur

A menu bar represents a list of menus which can be added to the top of a top-level window. Each menu is associated with a drop-down list of menu items. The concept of menu bar can be implemented by using three Java classes: JMenuBar, Menu and JMenuitem. A menu bar is represented by the object of class JMenuBar in which only the default constructor is defined. A menu bar may consist of one or more menus represented by the objects of the class JMenu. A menu contains a list of menu items represented by the objects of the class JMenuitem.

The JMenu class defines the following constructors.

JMenu ()  //first

JMenu(String optionName) //second

JMenu(String optionName, boolean removable) //third

The first constructor creates an empty menu. The second constructor creates a menu with the name of the menu specified by optionName. In the third constructor, removable may have one of the two values: true or false. Ifit is true then the menu can float freely in the application window, otherwise it remains attached to the menu bar.

The JMenuitem class defines the following constructors.

JMenuitem () //first

JMenuitem(String itemName)       //second

JMenuitem(String itemName, Icon icon)  //third

The first constructor creates a menu item with no name and no menu shortcut. In the second constructor, itemName specifies the name of the menu item. In the third constructor, icon specifies the icon associated with the menu item.

A checkable menu item can also be created by using a subclass of JMenuitem called JCheckboxMenuitem.

The JCheckboxMenuitem class defines the following constructors.

JCheckboxMenuitem()

JCheckboxMenuitem(String itemName)

JCheckboxMenuitem(String itemName, boolean checkable)

The first constructor creates an unchecked menu item with no name. The second constructor creates an unchecked menu item with the name of the menu item specified by itemName. In the third constructor, checkable can either be true or false. If it is true then the menu item is initially checked, otherwise it is unchecked.

There are two types of menus which are given below.

• Regular menus: They are placed at the top of the application window within a menu bar.

• Pop-up menus: They appear in the window when the user clicks. For example, a pop-up menu appears on right click of the mouse.

import javax.swing.*; 
import java.awt.*;
import java.awt.event.*;
public class JMenuBar2JavaExample extends JFrame
   {
         private JMenuBar MnuBar = new JMenuBar();
         private JMenu MnuOne = new JMenu("File");
         private JCheckBoxMenuItem chkOne = new  JCheckBoxMenuItem("Check Box First");
         private JCheckBoxMenuItem chkTwo = new JCheckBoxMenuItem("Check Box Second");
         private JRadioButtonMenuItem RdoOne = new JRadioButtonMenuItem("Radio Button One");
         private JRadioButtonMenuItem RdoTwo = new JRadioButtonMenuItem("Radio Button Two");
         private JRadioButtonMenuItem RdoThree = new JRadioButtonMenuItem("Radio Button Three");
         private ButtonGroup BtnGrp = new ButtonGroup();
         public JMenuBar2JavaExample()
         {
            setTitle("Menu Bar2 In java Swing");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLayout(new FlowLayout());
            setJMenuBar(MnuBar);
            MnuBar.add(MnuOne);
            MnuOne.add(chkOne);
            MnuOne.add(chkTwo);
            MnuOne.addSeparator();
            MnuOne.add(RdoOne);
            MnuOne.add(RdoTwo);
            MnuOne.add(RdoThree);
            BtnGrp.add(RdoOne);
            BtnGrp.add(RdoTwo);
            BtnGrp.add(RdoThree);
        }
            public static void main(String[] ds)
              {
                  JMenuBar2JavaExample frm = new JMenuBar2JavaExample();
                  final int WIDTH = 170;
                  final int HEIGHT = 220;
                  frm.setSize(500,500);
                  frm.setVisible(true);
             }
  }

MenuBar Example in Java Swing

You’ll also like:

  1. MenuBar class in Java Example
  2. CheckerBoard Java Swing Example
  3. showInputDialog Java Swing Example
  4. Sound in Java Swing Example
  5. setToolTipText() in Java Swing Example
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

SQL Tutorials

SQL Tutorials

  • SQL - Home
  • SQL - Select
  • SQL - Create
  • SQL - View
  • SQL - Sub Queries
  • SQL - Update
  • SQL - Delete
  • SQL - Order By
  • SQL - Select Distinct
  • SQL - Group By
  • SQL - Where Clause
  • SQL - Select Into
  • SQL - Insert Into
  • SQL - Sequence
  • SQL - Constraints
  • SQL - Alter
  • SQL - Date
  • SQL - Foreign Key
  • SQL - Like Operator
  • SQL - CHECK Constraint
  • SQL - Exists Operator
  • SQL - Drop Table
  • SQL - Alias Syntax
  • SQL - Primary Key
  • SQL - Not Null
  • SQL - Union Operator
  • SQL - Unique Constraint
  • SQL - Between Operator
  • SQL - Having Clause
  • SQL - Isnull() Function
  • SQL - IN Operator
  • SQL - Default Constraint
  • SQL - Minus Operator
  • SQL - Intersect Operator
  • SQL - Triggers
  • SQL - Cursors

Advanced SQL

  • SQL - Joins
  • SQL - Index
  • SQL - Self Join
  • SQL - Outer Join
  • SQL - Join Types
  • SQL - Cross Join
  • SQL - Left Outer Join
  • SQL - Right Join
  • SQL - Drop Index
  • SQL - Inner Join
  • SQL - Datediff() Function
  • SQL - NVL Function
  • SQL - Decode Function
  • SQL - Datepart() Function
  • SQL - Count Function
  • SQL - Getdate() Function
  • SQL - Cast() Function
  • SQL - Round() Function

Other Links

  • SQL - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW