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!
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.