Java – how do I move my jmenubar to the on-screen menu bar on Mac OS X?
When I move jmenubar to the on-screen menu bar on Mac OS X, it will leave some blank areas and the menu will appear in my window; I need to delete that space I'm using it
System.setProperty("apple.laf.useScreenMenuBar","true")
Move my jmenubar to the screen menu bar My friends use MAC reports. If I don't set this property, it will leave some ugly vertical space where the menu will reside What is the best way to solve this problem?
Editor: This is an example of my source:
public static void main(String[] args) { System.setProperty("apple.laf.useScreenMenuBar","true"); System.setProperty("com.apple.mrj.application.apple.menu.about.name","Name"); JFrame frame = new JFrame("Gabby"); final DesktopMain dm = new DesktopMain(); frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); frame.add(dm); frame.setSize(160,144); frame.setLocationRelativeTo(null); frame.setIgnoreRepaint(true); JMenuBar menuBar = new JMenuBar(); JMenu fileMenu = new JMenu("File"); menuBar.add(fileMenu); // Populating the menu bar code goes here frame.setJMenuBar(menuBar); frame.setVisible(true); }
Solution
Depending on the completion time, it may be too late to set the property after the program starts Instead, add settings at startup
java -Dapple.laf.useScreenMenuBar=true -jar MyApplication.jar
Or, in the application package's info Set this attribute in plist, such as Java deployment options for Mac OS X, Java dictionary info plist Keys,About Info. Plist keys and Java runtime system properties
<key>Properties</key> <dict> <key>apple.laf.useScreenMenuBar</key> <string>true</string> ... </dict>
Appendix: as shown below, there will be no problem using @ urs reupke or the method I suggest There may be a problem with your (missing) desktopmain
import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JPanel; /** @see https://stackoverflow.com/questions/8955638 */ public class NewMain { public static void main(String[] args) { System.setProperty("apple.laf.useScreenMenuBar","true"); System.setProperty( "com.apple.mrj.application.apple.menu.about.name","Name"); EventQueue.invokelater(new Runnable() { @Override public void run() { JFrame frame = new JFrame("Gabby"); final JPanel dm = new JPanel() { @Override public Dimension getPreferredSize() { return new Dimension(320,240); } }; dm.setBorder(BorderFactory.createLineBorder(Color.blue,10)); frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); frame.add(dm); frame.pack(); frame.setLocationByPlatform(true); JMenuBar menuBar = new JMenuBar(); JMenu fileMenu = new JMenu("File"); menuBar.add(fileMenu); frame.setJMenuBar(menuBar); frame.setVisible(true); } }); } }