JMenuBar does not enable after being disabled

2019-07-13 03:59发布

问题:

I'm disabling a JMenuBar before displaying a FileDialog (as the menu items are still active when the FileDialog is visible) using getJMenuBar().setEnabled(false) and then calling getJMenuBar().setEnabled(true) after the FileDialog closes, but the menu items do not become active after being enabled - they will if I change to another application and back to mine. I've tried calling getJMenuBar().revalidate() and/or getJMenuBar().repaint() to no avail.

Of note, I'm using a screen menu bar as I'm on OS X. Sample code that shows the problem:

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

public class MenuTest extends JFrame implements ActionListener {

    private JMenuItem menuItemNew = new JMenuItem("New");
    private JMenuItem menuItemOpen = new JMenuItem("Open");
    private JMenuItem menuItemSave = new JMenuItem("Save");
    private JMenu menuFile = new JMenu("File");
    private JMenuBar menuBar = new JMenuBar();

    public MenuTest() {
        super("JMenu Test");
        setSize(300, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        menuItemOpen.addActionListener(this);

        menuFile.add(menuItemNew);
        menuFile.add(menuItemOpen);
        menuFile.add(menuItemSave);
        menuBar.add(menuFile);
        setJMenuBar(menuBar);

        setVisible(true);
    }

    public void openFile() {
        getJMenuBar().setEnabled(false);
        FileDialog fd = new FileDialog(this, "Choose a file", FileDialog.LOAD);
        fd.setVisible(true);
        getJMenuBar().setEnabled(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == menuItemOpen) {
            openFile();
        }
    }

    public static void main(String[] arguements) {
        System.setProperty("apple.laf.useScreenMenuBar", "true");
        new MenuTest();
    }

}

Thanks in advance for comments / suggestions!

回答1:

For me, I could resolve the issue by enabling/disabling each JMenuItem and not the menu bar or menu itself.

I also did everything on the EDT, just in case that was the problem, but it didn't help.

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

public class MenuTest implements ActionListener {

    private JMenuItem menuItemNew = new JMenuItem("New");
    private JMenuItem menuItemOpen = new JMenuItem("Open");
    private JMenuItem menuItemSave = new JMenuItem("Save");
    private JMenu menuFile = new JMenu("File");
    private JMenuBar menuBar = new JMenuBar();
    JFrame frame;
    public MenuTest() {

    }

    public void buildGui(){
        frame = new JFrame("JMenu Test");
        frame.setSize(300, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        menuItemOpen.addActionListener(this);

        menuFile.add(menuItemNew);
        menuFile.add(menuItemOpen);
        menuFile.add(menuItemSave);

        menuBar.add(menuFile);
        frame.setJMenuBar(menuBar);

        frame.setVisible(true);
    }

    public void openFile() {
        menuItemNew.setEnabled(false);
        menuItemOpen.setEnabled(false);
        menuItemSave.setEnabled(false);

        FileDialog fd = new FileDialog(frame, "Choose a file", FileDialog.LOAD);
        fd.setVisible(true);

        menuItemNew.setEnabled(true);
        menuItemOpen.setEnabled(true);
        menuItemSave.setEnabled(true);

    }

    @Override
    public void actionPerformed(ActionEvent e) {

            openFile();
        }

    public static void main(String[] arguements) {
        System.setProperty("apple.laf.useScreenMenuBar", "true");
        EventQueue.invokeLater(()->{
            new MenuTest().buildGui();
                });
    }

}