Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 6th, 2012  |  syntax: None  |  size: 2.44 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How do I move my JMenuBar to the screen menu bar on Mac OS X?
  2. System.setProperty("apple.laf.useScreenMenuBar", "true")
  3.        
  4. public static void main(String[] args) {
  5.     System.setProperty("apple.laf.useScreenMenuBar", "true");
  6.     System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Name");
  7.  
  8.     JFrame frame = new JFrame("Gabby");
  9.     final DesktopMain dm = new DesktopMain();
  10.  
  11.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  12.     frame.add(dm);
  13.     frame.setSize(160, 144);
  14.     frame.setLocationRelativeTo(null);
  15.     frame.setIgnoreRepaint(true);
  16.  
  17.     JMenuBar menuBar = new JMenuBar();
  18.     JMenu fileMenu = new JMenu("File");
  19.     menuBar.add(fileMenu);
  20.  
  21.     // Populating the menu bar code goes here
  22.  
  23.     frame.setJMenuBar(menuBar);
  24.     frame.setVisible(true);
  25. }
  26.        
  27. java -Dapple.laf.useScreenMenuBar=true -jar MyApplication.jar
  28.        
  29. <key>Properties</key>
  30. <dict>
  31.     <key>apple.laf.useScreenMenuBar</key>
  32.     <string>true</string>
  33.     ...
  34. </dict>
  35.        
  36. import java.awt.Color;
  37. import java.awt.Dimension;
  38. import java.awt.EventQueue;
  39. import javax.swing.BorderFactory;
  40. import javax.swing.JFrame;
  41. import javax.swing.JMenu;
  42. import javax.swing.JMenuBar;
  43. import javax.swing.JPanel;
  44.  
  45. /** @see http://stackoverflow.com/questions/8955638 */
  46. public class NewMain {
  47.  
  48.     public static void main(String[] args) {
  49.         System.setProperty("apple.laf.useScreenMenuBar", "true");
  50.         System.setProperty(
  51.             "com.apple.mrj.application.apple.menu.about.name", "Name");
  52.         EventQueue.invokeLater(new Runnable() {
  53.  
  54.             @Override
  55.             public void run() {
  56.  
  57.                 JFrame frame = new JFrame("Gabby");
  58.                 final JPanel dm = new JPanel() {
  59.  
  60.                     @Override
  61.                     public Dimension getPreferredSize() {
  62.                         return new Dimension(320, 240);
  63.                     }
  64.                 };
  65.                 dm.setBorder(BorderFactory.createLineBorder(Color.blue, 10));
  66.  
  67.                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  68.                 frame.add(dm);
  69.                 frame.pack();
  70.                 frame.setLocationByPlatform(true);
  71.  
  72.                 JMenuBar menuBar = new JMenuBar();
  73.                 JMenu fileMenu = new JMenu("File");
  74.                 menuBar.add(fileMenu);
  75.                 frame.setJMenuBar(menuBar);
  76.                 frame.setVisible(true);
  77.             }
  78.         });
  79.     }
  80. }
  81.        
  82. System.setProperty("com.apple.mrj.application.apple.menu.about.name", "MyApplication");