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 8959057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:21:37+00:00 2026-06-15T15:21:37+00:00

I have a JFrame with a JMenuBar that I’m developing and testing on a

  • 0

I have a JFrame with a JMenuBar that I’m developing and testing on a Mac system. On this JFrame, I have programmed one of the JMenus to be disabled. However, when I change focus from my Java application to some other program on my computer and then come back to this JFrame, strange things are happening. I have observed that all of the menus become disabled. I have also observed that all of the menus become enabled. Can anyone tell me what’s going on here?

Here’s a piece of code that will reproduce the error (at least it does on my machine):

public class MenuProblemExample {

    public static void main(String[] args) {
        System.setProperty("apple.laf.useScreenMenuBar", "true");
        JFrame frame = new JFrame();
        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        fileMenu.add(new JMenuItem("open"));
        menuBar.add(fileMenu);
        JMenu editMenu = new JMenu("Edit");
        editMenu.add(new JMenuItem("select all"));
        menuBar.add(fileMenu);
        menuBar.add(editMenu);
        frame.setJMenuBar(menuBar);
        fileMenu.setEnabled(false);
        frame.setVisible(true);        
    }
}

When I run this, the enabled property isn’t stable under the action of switching focus to another window and then back again.

  • 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-15T15:21:39+00:00Added an answer on June 15, 2026 at 3:21 pm

    Swing GUI objects should be constructed and manipulated only on the event dispatch thread.

    By design, Mac applications using the screen menubar disable menus in the background. It is up to your application to enable menus appropriately when your application comes to the foreground, using e.g. a WindowListener.

    Addendum: For testing, this revised example adds a toggle button that tracks the state of the File > Open menu item.

    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import javax.swing.AbstractAction;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JToggleButton;
    
    /**
     * @see http://stackoverflow.com/a/13756527/230513
     */
    public class MenuProblemExample {
    
        public static void main(String[] args) {
            System.setProperty("apple.laf.useScreenMenuBar", "true");
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    JMenuBar menuBar = new JMenuBar();
                    JMenu fileMenu = new JMenu("File");
                    final JMenuItem openItem = new JMenuItem("open");
                    openItem.setEnabled(false);
                    fileMenu.add(openItem);
                    menuBar.add(fileMenu);
                    JMenu editMenu = new JMenu("Edit");
                    editMenu.add(new JMenuItem("select all"));
                    menuBar.add(fileMenu);
                    menuBar.add(editMenu);
                    frame.setJMenuBar(menuBar);
                    frame.add(new JToggleButton(new AbstractAction("Toggle") {
    
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            JToggleButton b = (JToggleButton) e.getSource();
                            openItem.setEnabled(b.isSelected());
                        }
                    }));
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JFrame with null layout and two one character JLabels on the
I have code that displays a menu and 4 JButtons in a JFrame. I
INTRO: I created a java application using JFrame. I have a JMenuBar at the
I have a MainWindow class that extends JFrame and is the only frame in
I have a Jframe class that has a login and password fields. When loggin
I have a JFrame that displays a Java icon on the title bar (left
I have created a program that has a JScrollPane . However, JScrollPane doesn't seem
I have a JFrame and a Jpanel over that in which various buttons are
I have a JFrame inside of which is a jpanel that im using as
I have a JFrame with 1 panel for drawing and set KyeListener for JFrame.

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.