I am trying to adjust the colors of the Nimbus Look and Feel but it is only working partially. Especially I have problems adjusting the colors of the menubar.
Here is a running example:
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;
public class JMenuColorTest extends JFrame {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    adjustLAF();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                JMenuColorTest test = new JMenuColorTest();
                test.setDefaultCloseOperation(EXIT_ON_CLOSE);
                test.setPreferredSize(new Dimension(400, 300));
                test.pack();
                test.setLocationRelativeTo(null);
                JMenuBar menuBar = new JMenuBar();
                JMenu menu1 = new JMenu("Menu 1");
                menu1.add(new JMenuItem("Item 1.1"));
                menu1.add(new JMenuItem("Item 1.2"));
                menu1.add(new JMenuItem("Item 1.3"));
                menuBar.add(menu1);
                JMenu menu2 = new JMenu("Menu 2");
                menu2.add(new JMenuItem("Item 2.1"));
                menu2.add(new JMenuItem("Item 2.2"));
                menu2.add(new JMenuItem("Item 2.3"));
                menuBar.add(menu2);
                test.setJMenuBar(menuBar);
                test.setVisible(true);
            }
            private void adjustLAF() throws ClassNotFoundException,
                InstantiationException, IllegalAccessException,
                UnsupportedLookAndFeelException {
                for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        // Working
                        UIManager.put("control", Color.GREEN);
                        // Not working
                        UIManager.getLookAndFeelDefaults().put(
                            "MenuItem[Enabled].textForeground", Color.RED);
                        // Set the look and feel
                        UIManager.setLookAndFeel(info.getClassName());
                        // Not working
                        UIManager.put("control", Color.GREEN);
                        // Working
                        UIManager.getLookAndFeelDefaults().put(
                            "MenuItem[Enabled].textForeground", Color.RED);
                        break;
                    }
                }
            }
        });
    }
}
As you can see I am able to set background of the controls and set the foreground color of the JMenuItem. But I am not able to change the background of a JMenuItem, neither I am able to change the colors of the MenuBar. I tried a lot of keys from http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/_nimbusDefaults.html but I was not able to change the color of the menubar.
Another question is? Why do I have to call the adjusting of the colors once before setting the Look and Feel and once after setting the Look and Feel? And why do I have to call once 'UIManager.put()' and once 'UIManager.getLookAndFeelDefaults().put()'?
It seems to me that Nimbus is really buggy and not suitable for professional use. I tried to use both JDK 1.6.35 and JDK 1.7.7, but with both JDKs I could not get the system running as desired?
Any suggestions how to adjust the colors of a menubar in Nimbus LookAnd Feel?
Thanks in advance
for JMenuBar to have to use Painter, to check NimbusDefault#value
MenuBar[Enabled].backgroundPainter
MenuBar[Enabled].borderPainter
rest is in answer by trashgod +1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With