JButton on a Mac

By: Nicholas Duchon
About GUI Look and Feel options



import javax.swing.UIManager;
import javax.swing.SwingUtilities;
import javax.swing.JPanel;
import javax.swing.JButton;

List the looks available on your system:

try {
   System.out.println ("look and feels:");
   for (Object o: UIManager.getInstalledLookAndFeels())
      System.out.println (o.toString());
} catch (Exception e) {

My system showed the following:

look and feels:
javax.swing.UIManager$LookAndFeelInfo[Metal javax.swing.plaf.metal.MetalLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[Nimbus javax.swing.plaf.nimbus.NimbusLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[CDE/Motif com.sun.java.swing.plaf.motif.MotifLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[Mac OS X com.apple.laf.AquaLookAndFeel]

Thus, I defined a String for each look:

static String metal  = "javax.swing.plaf.metal.MetalLookAndFeel";
static String nimbus = "javax.swing.plaf.nimbus.NimbusLookAndFeel";
static String motif  = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
static String macosx = "com.apple.laf.AquaLookAndFeel";

Then set up JButtons to let the user change the look at run time:

JPanel jpUIControls = new JPanel ();
JButton jbUIA = new JButton ("Metal");
JButton jbUIB = new JButton ("Nimbus");
JButton jbUIC = new JButton ("Motif");
JButton jbUID = new JButton ("MacOSX");
jpUIControls.add (jbUIA);
jpUIControls.add (jbUIB);
jpUIControls.add (jbUIC);
jpUIControls.add (jbUID);
jbUIA.addActionListener (e -> setFeel (metal));
jbUIB.addActionListener (e -> setFeel (nimbus));
jbUIC.addActionListener (e -> setFeel (motif));
jbUID.addActionListener (e -> setFeel (macosx));
setFeel (metal); // gets button colors to show on mac for all feels!?!?!?

And added a method to actually do the work

void setFeel (String s) {
   try {
   //          UIManager.setLookAndFeel(
   //             UIManager.getCrossPlatformLookAndFeelClassName() );
      UIManager.setLookAndFeel (s);
   } catch (Exception e) {
} // end setFeel


Earlier Suggestions

if (background != null) {
  button.setOpaque (true);         // ND: getting this to work on a MAC
  button.setBorderPainted (false); // ND: This is needed as well!
Or try this: from: java - How to Set the Background Color of a JButton on the Mac OS - Stack Overflow

If you are not required to use Apple's look and feel, a simple fix is to put the following code in your application or applet,
you add any GUI components to your JFrame or JApplet:

try {
javax.swing.UIManager.setLookAndFeel( javax.swing.UIManager.getCrossPlatformLookAndFeelClassName() );
} catch (Exception e) {

That will set the look and feel to the cross-platform look and feel, and the setBackground() method will then work to change a JButton's background color.