How do I move my JMenuBar to the screen menu bar on Mac OS X?
Asked Answered
H

4

8

When I move my JMenuBar to the screen menu bar on Mac OS X, it leaves some blank space where the menu would be in my window; I need to remove that space. I am using

System.setProperty("apple.laf.useScreenMenuBar", "true")

to move my JMenuBar to the screen menu bar. My friend who uses a Mac reports that this leaves some ugly vertical space where the menu would reside if I did not set that property. What is the best way to resolve this issue?

EDIT: Here is an example from 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);
}
Hydnocarpate answered 21/1, 2012 at 18:45 Comment(1)
Could you post code? Normally the layout adjusts automatically to use all the room.Betide
M
13

Depending on when it's done, setting the property after your program launches may be too late to be effective. Instead, add the setting at launch time.

java -Dapple.laf.useScreenMenuBar=true -jar MyApplication.jar

Alternatively, set the property in your application bundle's Info.plist, as discussed in 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>

Addendum: As shown below, the problem does not appear using the approach suggested by @Urs Reupke or myself. Your (missing) DesktopMain may be at fault.

Screen capture

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 http://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);
            }
        });
    }
}
Mintamintage answered 21/1, 2012 at 21:11 Comment(2)
trashgod, that doesn't seem right - I'm doing it in my code, and I have yet to hear a complaint.Demission
@UrsReupke: Good point; edited. I can't reproduce the effect, but I've seen reports like this.Mintamintage
D
4

As an alternative to what trashgod suggests, do what you're doing - but do it very early, before you initialize your UI.

Also, you might consider using this to display your application's name in the bar:

    System.setProperty("com.apple.mrj.application.apple.menu.about.name", "MyApplication");
Demission answered 21/1, 2012 at 21:21 Comment(2)
+1 tnecniv: Please provide an sscce that exhibits the problem you describe.Mintamintage
@Mintamintage I added my source exampleHydnocarpate
W
4

I know this question is old, but what worked for me is I created a new class called Main, and then set the system property like

System.setProperty("apple.laf.useScreenMenuBar", "true");
System.setProperty("apple.awt.application.name", "My app");

Works in JDK15.

Wildon answered 11/8, 2020 at 19:40 Comment(1)
It needs to be set before any Swing class gets initialized.Mazer
D
0

For Java 8 use this

System.setProperty("apple.awt.application.name", "My app");
Discoloration answered 27/11, 2016 at 1:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.