Skip to content

Instantly share code, notes, and snippets.

@ctrueden
Last active December 16, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctrueden/5444972 to your computer and use it in GitHub Desktop.
Save ctrueden/5444972 to your computer and use it in GitHub Desktop.
Can you reuse the same JMenuBar on two different JFrames on OS X as a screen menu bar? (Answer: no.)
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.WindowConstants;
/**
* On OS X, with a screen menu bar, you cannot reuse a JMenuBar between two
* JFrames. Upon adding it to the second, it is removed from the first.
*/
public class ReuseJMenuBarOSX {
public static void main(final String[] args) {
System.setProperty("apple.laf.useScreenMenuBar", "true");
final JMenuBar menuBar = new JMenuBar();
final JMenu file = new JMenu("File");
menuBar.add(file);
final JMenuItem fileNew = new JMenuItem("New");
file.add(fileNew);
final JFrame frame1 = new JFrame("First");
frame1.getContentPane().add(new JButton("First"));
frame1.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame1.setJMenuBar(menuBar);
final JFrame frame2 = new JFrame("Second");
frame2.getContentPane().add(new JButton("Second"));
frame2.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame2.setJMenuBar(menuBar);
final int offsetX = 200, offsetY = 50;
frame1.pack();
frame1.setLocation(offsetX, offsetY);
frame1.setVisible(true);
frame2.pack();
frame2.setLocation(frame1.getWidth() + offsetX + 10, offsetY);
frame2.setVisible(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment