Search code examples
javamacosswingjmenubar

How do I put the JMenuBar on top in mac and change the background of a JButton


I am building a Mac desktop application in java. I want to change the background of a JButton and also I want to make my JMenuBar be in the top.

To put my JMenuBar on the top I added this code:

  System.setProperty("apple.laf.useScreenMenuBar", "true");
  System.setProperty(
      "com.apple.mrj.application.apple.menu.about.name", "Stack");

And it worked!

And to change the backgroud color I used this:

    JButton b = new JButton("press me!");

    b.setBackground(Color.blue);

    b.setContentAreaFilled(false);
    b.setOpaque(true);
    b.setBorderPainted(true);
    try {
        UIManager.setLookAndFeel(UIManager
                .getCrossPlatformLookAndFeelClassName());
    } catch (Exception e) {

    }

And it also worked!

The problem was that when I changed the color the JMenuBar was not in the top. After a little debugging I know that changing the LookAndFeel is the responsable.

The complete code:

import java.awt.Color;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.UIManager;

public class Main {
    public static void main(String[] args) {    
        System.setProperty("apple.laf.useScreenMenuBar", "true");
        System.setProperty(
          "com.apple.mrj.application.apple.menu.about.name", "Stack");

        JButton b = new JButton("press me!");           
        b.setBackground(Color.blue);    
        b.setContentAreaFilled(false);
        b.setOpaque(true);
        b.setBorderPainted(true);
        try {
            UIManager.setLookAndFeel(UIManager
                    .getCrossPlatformLookAndFeelClassName());
        } catch (Exception e) {

        }   
        JFrame f = new JFrame();            
        f.add(b);           
        JMenuBar m = new JMenuBar();            
        m.add(new JMenu("item"));   
        f.setJMenuBar(m);
        f.setVisible(true);         
        f.setVisible(true);
    }
}

So this code change the color of the button but the JMenuBar is not in the top. If you comment the lines in the try it will not change the color but it will put the JMenuBar on the top.

Any help??

Thanks in advance!


Solution

  • I solved this by adding the line

    b.setUI(new MetalButtonUI());
    

    and deleting the try catch.

    It ended up looking like these:

    import java.awt.Color;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.UIManager;
    import javax.swing.plaf.metal.MetalButtonUI;
    
    public class Main {
    
        public static void main(String[] args) {
    
            System.setProperty("apple.laf.useScreenMenuBar", "true");
            System.setProperty("com.apple.mrj.application.apple.menu.about.name",
                    "Stack");
            JButton b = new JButton("press me!");
    
            b.setBackground(Color.blue);
    
            b.setContentAreaFilled(false);
            b.setOpaque(true);
            b.setBorderPainted(true);
            b.setUI(new MetalButtonUI());
    
    
            JFrame f = new JFrame();
    
            f.add(b);
    
    
    
            f.setBounds(0, 0, 500, 500);
            JMenuBar m = new JMenuBar();
    
            m.add(new JMenu("item"));
    
    
    
            f.setJMenuBar(m);
            f.setVisible(true);
    
            f.setVisible(true);
        }
    }