Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8827415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:21:55+00:00 2026-06-14T07:21:55+00:00

I have a Java program in which I enable and disable menus. It works

  • 0

I have a Java program in which I enable and disable menus. It works nicely under Windows, but I have some problems when running it on a Mac.

Here’s a piece of code that demonstrates the problem:

import javax.swing.*;
import java.awt.event.*;

public class PopTest extends JFrame {
    JMenu menu1;
    JMenu menu2;

    public PopTest() {
        menu1 = new JMenu("Menu 1");
        menu2 = new JMenu("Menu 2");
        menu2.setEnabled(false);

        menu1.add(new JMenuItem(new AbstractAction("With popup") {
                public void actionPerformed(ActionEvent event) {
                    JOptionPane.showMessageDialog(PopTest.this,"Popup","Popup",JOptionPane.ERROR_MESSAGE);
                    menu2.setEnabled(true);
                    menu2.add(new JMenuItem("New item"));
                }
            }));

        menu1.add(new JMenuItem(new AbstractAction("Without popup") {
                public void actionPerformed(ActionEvent event) {
                    menu2.setEnabled(true);
                    menu2.add(new JMenuItem("New item"));
                }
            }));

        JMenuBar menuBar = new JMenuBar();
        menuBar.add(menu1);
        menuBar.add(menu2);
        setJMenuBar(menuBar);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(200, 200);
        setVisible(true);
    }

    public static void main(String[] args) {
        try {
            System.setProperty("apple.laf.useScreenMenuBar", "true");
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch(Exception e) {
            System.out.println("Exception: " + e.getMessage());
        }

        SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new PopTest();
                }
            });

    }
}

This is what the program does:

The program has two menus, “Menu 1” and “Menu 2”. From the outset, Menu 1 is enabled and Menu 2 is disabled.

Menu 1 has two items that do almost the same thing: They enable Menu 2 and add a menu item to it. The difference is that one of the items display a message dialog before enabling Menu 2, whereas the other item does not.

Now, compile the program and try this:

Experiment 1: Select Menu 1 > Without popup. Now click on Menu 2 and you will see that the menu has an enabled menu item called “New item”.

Expermiment 2: Close the program! Start the program again. Select Menu 1 > With popup. Click OK in the message dialog. Now click on Menu 2 and you will see that the menu has a disabled menu item called “New item”. Click elsewhere on your desktop so that the application looses focus. Click in the application. Click on Menu 2 and you will see that the menu item is now enabled.

In experiment 2 it is very important that the application does not loose focus before you are instructed to click elsewhere on your desktop. Otherwise you will not see the problem.

Why is the menu item in Menu 2 disabled in the second experiment? Is this a bug? I’m using OS X 10.8.2 (Mountain Lion) and Java 1.7.0_09.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:21:56+00:00Added an answer on June 14, 2026 at 7:21 am

    I can replicate the issue in Java 7, but not Java 6. I don’t understand why it’s doing this, but one thing that fixed it for me was to move setEnabled() above JOptionPane.showMessageDialog():

        menu1.add(new JMenuItem(new AbstractAction("With popup")
        {
            public void actionPerformed(ActionEvent event)
            {
                menu2.setEnabled(true);
                JOptionPane.showMessageDialog(Test.this, "Popup", "Popup", JOptionPane.ERROR_MESSAGE);
                menu2.add(new JMenuItem("New item"));
            }
        }));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I now have a running Java program which only lacks of the final step,that
I have a program which i have myself written in java, but I want
Unix gurus! I have a Java program which passes some parameters to a Servlet
I have created a Java program which takes some command line arguments to run.
I have a Java program which is using WMIC to get certain information about
I have a java program which sends a udp packet to a server when
I have a Java program which prepares data into a fairly complex and big
I have a Java 3D program which uses lots (hundreds) of 3D models (3ds
I have a Java program in which an action is initiated which loops through
I have a Java program in which I have to validate an XML message

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.