3

After implementing Mac OS X look and feel, just after reading

I noticed two problems.

  1. While the JMenuBar is now shown in the Mac Bar, if I click on a JMenuItem, no event is called. Using: System.setProperty("apple.laf.useScreenMenuBar", "true");
  2. The name shown in the bar using System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Name"); is the name given in the cration of the project.

The interface: GUI


I'm trying Java OS X Lion Set application name doesn't work to solve the second problem. I' can't launch the jar from command line, so I'm using Martjin answer.

Community
  • 1
  • 1
Mitro
  • 1,230
  • 8
  • 32
  • 61

1 Answers1

2

Starting from the example cited,

  1. The JMenuItem listener prints "Here" when File > Item is clicked.

  2. The about.name property is the name in the About dialog, shown below, but the property is currently ignored.

image

As tested:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

/**
 * @see https://stackoverflow.com/a/22766921/230513
 * @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");
                JMenuItem item = new JMenuItem("Item");
                fileMenu.add(item);
                item.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("Here");
                    }
                });
                menuBar.add(fileMenu);
                frame.setJMenuBar(menuBar);
                frame.setVisible(true);
            }
        });
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I've copied this code, as it has been written, now event is called, but the name doesn't change. +1 but not full yet. Infact, as you can see in your answer, the title shown is NewMain instead of Name. – Mitro Mar 31 '14 at 19:37
  • Right; to to get the name you want, you'll have to create an application bundle and specify the name in `Info.plist`. – trashgod Apr 01 '14 at 00:13
  • Where do I find Info.plist?, I see where is in Xcode, but I use NetBeans. – Mitro Apr 01 '14 at 18:03
  • `Info.plist` goes in your application bundle; see [*Java Deployment Options for OS X*](https://developer.apple.com/library/mac/documentation/java/conceptual/java14development/03-javadeployment/javadeployment.html) for details. – trashgod Apr 01 '14 at 20:41