How to change background color for nimbus using java

In a Java Swing application, I try to look and feel in it. It looks great in the JdesktopPane control, but I need a different color for my entire desktop, but the theme is beautiful.

Is there any way to change the background color of nimbus?

Here is some sample code to apply the appearance of a halo.

   try {
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
          if ("Nimbus".equals(info.getName())) {
          UIManager.setLookAndFeel(info.getClassName());
          break;
         }
        }
       }  
     catch (Exception e) {}

      

+3


source to share


2 answers


UIManager.put("nimbusBase", new Color(...));
UIManager.put("nimbusBlueGrey", new Color(...));
UIManager.put("control", new Color(...)) 

      



from the l & f tutorial

+3


source


The nimbus paints the background using what looks like a vector-type drawing routine to draw a fancy background pattern. To change the background of the JDesktopPane in the case of nimbus, you need to change the background Painter used by the JDesktopPane to simply fill the panel with the background color you want (like gray). Then set the property "DesktopPane[Enabled].backgroundPainter"

using this Painter object.For example, see the below code:



import java.awt.Graphics2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JDesktopPane;
import javax.swing.SwingUtilities;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import com.sun.java.swing.Painter;
import java.awt.Color;

public class NimbusFrame extends JFrame
{
    private JDesktopPane desktop;
    public void prepareAndShowGUI()
    {
        desktop = new MyDesktopPane();
        getContentPane().add(desktop);
        setSize(300,400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    class MyDesktopPane extends JDesktopPane
    {
        @Override
        public void updateUI() 
        {
            if ("Nimbus".equals(UIManager.getLookAndFeel().getName())) 
            {
                UIDefaults map = new UIDefaults();
                Painter<JComponent> painter = new Painter<JComponent>() 
                {
                    @Override
                    public void paint(Graphics2D g, JComponent c, int w, int h) 
                    {
                        g.setColor(Color.gray);
                        g.fillRect(0, 0, w, h);
                    }
                };
                map.put("DesktopPane[Enabled].backgroundPainter", painter);
                putClientProperty("Nimbus.Overrides", map);
            }
            super.updateUI();
        }
    }
    public static void main(String st[])
    {
        try
        {
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) 
            {
                    if ("Nimbus".equalsIgnoreCase(info.getName())) 
                {
                        UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
        }catch(Exception ex){}
        SwingUtilities.invokeLater( new Runnable()
        {
            public void run()
            {
                NimbusFrame frame = new NimbusFrame();
                frame.prepareAndShowGUI();
            }
        });
    }
}

      

+2


source







All Articles