Created
April 23, 2013 16:30
-
-
Save ctrueden/5445168 to your computer and use it in GitHub Desktop.
The Application.setDefaultMenuBar(JMenuBar) method prevents the JVM from shutting down properly afterward.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.apple.eawt.Application; | |
import java.awt.event.WindowAdapter; | |
import java.awt.event.WindowEvent; | |
import javax.swing.JButton; | |
import javax.swing.JFrame; | |
import javax.swing.JMenu; | |
import javax.swing.JMenuBar; | |
import javax.swing.JMenuItem; | |
/** | |
* On OS X, with a screen menu bar, the | |
* {@link Application#setDefaultMenuBar(JMenuBar)} method is very useful for | |
* presenting a consistent menu bar between JFrames. But it seems that the JVM | |
* will does shut down cleanly afterward, even if the default menu bar is reset | |
* to null when the window is closed. | |
*/ | |
public class OSXApplicationDisposal { | |
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); | |
Application.getApplication().setDefaultMenuBar(menuBar); | |
final JFrame frame = new JFrame("My App"); | |
frame.getContentPane().add(new JButton("Button")); | |
frame.addWindowListener(new WindowAdapter() { | |
@Override | |
public void windowClosing(final WindowEvent e) { | |
frame.dispose(); | |
Application.getApplication().setDefaultMenuBar(null); | |
} | |
}); | |
frame.pack(); | |
frame.setLocation(200, 50); | |
frame.setVisible(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment