Swing application menu name not displaying correctly in Java 1.8
Asked Answered
S

1

5

Okay, so I've done Swing applications before, and I know if you want to display a different name for the application menu (the one on Macs that usually have a "Preferences" and "Quit" option), you have to use: System.setProperty("com.apple.mrj.application.apple.menu.about.name", "App name"); and it must be executed before the JFrame is created. I've done this, but it continues to show my Main class' name as the menu name, as if I didn't write that line of code at all. I googled for this issue, but couldn't find anything useful, and then I just searched on here, but everyone who had a similar issue was running Java 1.5, 1.6, or 1.7. So I thought maybe it had something to do with my current Java version, 1.8.

This, this, and this did not work. This, this, and this either sent me to outdated info, or the links didn't work anymore. Also, I'm running Mac 10.8.

Any suggestions/answers would be greatly appreciated.

An update:

here is the code I originally had:

package bouncing_off_axes;

/**
 * This is the driver class of this program.
 * 
 * @author Mason 
 *
 */

public class Main {

    /**
     * The driving method.
     * 
     * @param args
     */

    public static void main(String[] args) {

        System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Physics Engine Practice - Bouncing Balls");
        SimulationController view = new SimulationController("Test");

    }

}

And here is a solution that trashgod provided to someone else:

package bouncing_off_axes;

    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);
                }
            });
        }
    }

And apparently I need 10 reputation to post images, so I can't show you the result, but it didn't work out.

Stallfeed answered 26/9, 2014 at 22:41 Comment(5)
This works for me on 10.9, Java 8. For more specific help, please edit your question to include a complete example that exhibits the problem you describe.Wounded
@Wounded I copied verbatim the solution that you posted here. The only difference is that I have the class in a non-default package, and I have the reference package bouncing_off_axes in the code. It did not work, the menu continued to display the class name and not the name that was set. I'll edit my post and put what I originally had and the example you provided. Also, I'd like to make this change in the code, and not on the command line.Stallfeed
AFAIK, the setProperty() approach fails with Java 8. Why not add your JAR to an application bundle, for example?Wounded
Ah, I see. I thought that might be the case. I was hoping not to have to add it to an application bundle, but if that's the only way to go, I may just hold off on it until I actually finish my project. This one in particular was just me doing some physics engine practice so when I'm coding the game I intend to work on, it can all be fresher in my mind.Stallfeed
I've elaborated below.Wounded
W
7

Using Java 8 on Mac OS X 10.9, setting the System property

System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Name");

appears to be ineffectual in changing the name displayed in the application menu. Alternatives that still work include these:

  • By default, the application menu will display the simple name of the class specified in the java command line or the Main-Class attribute of the JAR's manifest.

     java -cp build/classes mypackage.Name
     java -jar dist/MyJar.jar
    
  • Use the -Xdock:name="Name" command line parameter.

    java -Xdock:name="Name" -cp build/classes mypackage.Main
    java -Xdock:name="Name" -jar MyJar.jar
    
  • Add the application JAR to a Mac application bundle, as shown here, an add the attribute to the Info.plist.

    <key>JVMOptions</key>
    <string>-Xdock:name=Name</string>
    
Wounded answered 28/9, 2014 at 0:51 Comment(2)
I am creating my jar using the gradle plugin. Is it possible to name the application with that approach? Currently it just picks up the name of the main class.Bailar
As all three approaches shown are extra-linguistic, platform-dependent mechanisms, I don't know why not; you might ask in gradle.Wounded

© 2022 - 2024 — McMap. All rights reserved.