- Menu Bar Test Program

This section provides a tutorial example on how to use the javax.swing.JMenuBar class to create a menu bar in a frame window. Menus and menu items added to the menu bar will be listed horizontally.

Here is an example program I wrote to test the JMenuBar class:

 * Copyright (c) 1997-2024 All Rights Reserved.
import java.awt.event.*;
import javax.swing.*;
public class JMenuBarTest {
   public static void main(String[] a) {
      JFrame f = new JFrame("JMenuBar Test");

      JMenuBar mb = new JMenuBar();      
      mb.add(new JMenu("Tools")); 
      mb.add(new JMenu("Options")); 
      mb.add(new JMenuItem("Save")); 
      mb.add(new JMenuItem("Quit")); 
      mb.add(new JButton("Stop")); 
      f.getContentPane().add(new MyButton());
   private static class MyButton extends JButton 
      implements ActionListener {
      public MyButton() {
      public void actionPerformed(ActionEvent e) {
         System.out.println("Check button clicked");
         JFrame myFrame = (JFrame) 
         JMenuBar myMenuBar = myFrame.getJMenuBar();
         System.out.println("# of elements in the menu bar: "
         System.out.println("Is the menu bar selected: "

If you run this example, you will see the frame window shows up with the menu bar like this:

Menu Bar Listeners
Menu Bar Listeners

If you click the "Check" button, click the "Options" menu in the menu bar, and click the "Check" button again, you will see text output in the console window:

Check button clicked
# of elements in the menu bar: 5
Is the menu bar selected: false
Check button clicked
# of elements in the menu bar: 5
Is the menu bar selected: true

Interesting notes about this tutorial example:

Table of Contents

 About This Book

 JDK (Java Development Kit)

 Introduction of Java Swing Package

 Graphics Environment of the Local System

 JFrame - Main Frame Class

 JLabel - Swing Label Class

 JButton - Swing Button Class

 JCheckBox - Swing Check Box Class

 JRadioButton - Swing Radio Button Class

 JTextField - Swing Text Field Class

 JComboBox - Swing Combo Box Class

Menu Bar, Menus, Menu Items and Listeners

 JMenuBar, JMenu, and JMenuItem Classes - Menu Bar Test Program - Menu Test Program - Menu Item Test Program - Radio Button Menu Item Test Program - Check Box Menu Item Test Program

 javax.swing.event.MenuListener - Menu Listener Interface - Menu Item Action Listener Test

 Item Listener on Radio Button Menu Items

 Item Listener on Check Box Menu Items

 javax.swing.event.MenuKeyListener - Menu Key Listener Interface

 setMnemonic() - Setting Keyboard Mnemonics on Menu Items

 setAccelerator() - Setting Keyboard Accelerators on Menu Items

 setMnemonic() - Setting Keyboard Mnemonics on Menus

 Creating Internal Frames inside the Main Frame

 Layout of Components in a Container

 LookAndFeel and UIManager

 Option Dialog Boxes

 JEditorPane - The Editor Pane Class

 SwingWorker - The Background Task Worker

 AWT (Abstract Windows Toolkit)

 Integration with Desktop System

 Archived Tutorials


 Full Version in PDF/EPUB