How to change background color for JPanels using Nimbus Look and Feel?

I want to use a different background color for all my JPanels in the application. How can I do this when using Nimbus Look and Feel?

I follow Changing the color theme to change the color of components in Nimbus Look and Feel.

It only works occasionally, randomly . If I set the PropertyChagneListener before changing the color, it will be notified only once .

Here are some test codes:

 public class RedPanels extends JFrame { public RedPanels() { JPanel panel = new JPanel(); add(panel); setPreferredSize(new Dimension(100, 100)); pack(); setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { try { for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { UIManager.setLookAndFeel(info.getClassName()); UIManager.getDefaults().addPropertyChangeListener( new PropertyChangeListener() { @Override public void propertyChange(PropertyChangeEvent event) { if (event.getPropertyName().equals("Panel.background")) { System.out.println("color changed"); } }); UIManager.put("Panel.background", new Color(255,0,0)); break; } } } catch (Exception e) { // Nimbus is not available. } new RedPanels(); } }); } } 
+4
source share
3 answers
 UIManager.getLookAndFeelDefaults().put("Panel.background", Color.RED); 
+5
source

There are three ways:

1) override nimbusBase for DerivedColor set

2) create your own Painter , just one example -> aephyr codesource ,

3) a simple and dirty hack to set the color directly

enter image description here

 import java.awt.*; import javax.swing.*; import javax.swing.border.LineBorder; public class NimbusJPanelBackGround { public NimbusJPanelBackGround() { JPanel p = new JPanel(); UIDefaults nimbusOverrides = new UIDefaults(); nimbusOverrides.put("Panel.background", Color.blue); p.putClientProperty("Nimbus.Overrides", nimbusOverrides); SwingUtilities.updateComponentTreeUI(p); JPanel p1 = new JPanel(); nimbusOverrides = new UIDefaults(); nimbusOverrides.put("Panel.background", Color.green); p1.putClientProperty("Nimbus.Overrides", nimbusOverrides); SwingUtilities.updateComponentTreeUI(p1); p1.setBorder(new LineBorder(Color.black, 1)); JPanel p2 = new JPanel(); nimbusOverrides = new UIDefaults(); nimbusOverrides.put("Panel.background", Color.ORANGE); p2.putClientProperty("Nimbus.Overrides", nimbusOverrides); SwingUtilities.updateComponentTreeUI(p2); JFrame f = new JFrame(); f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); f.add(p, BorderLayout.NORTH); f.add(p1, BorderLayout.CENTER); f.add(p2, BorderLayout.SOUTH); f.setSize(200, 100); f.setLocation(150, 150); f.setVisible(true); } public static void main(String[] args) { try { for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(laf.getName())) { UIManager.setLookAndFeel(laf.getClassName()); } } } catch (Exception e) { e.printStackTrace(); } EventQueue.invokeLater(new Runnable() { @Override public void run() { NimbusJPanelBackGround nimbusJPanelBackGround = new NimbusJPanelBackGround(); } }); } } 
+2
source

Looks like an error in jdk6, Panel.background is one of the properties that were not executed. After working in jdk7 (note the sequence: first set the color, then LAF)

  UIManager.put("Panel.background", new Color(255,0,0)); UIManager.setLookAndFeel(info.getClassName()); 

I assume that it is still somehow buggy, since Nimbus should update its properties when it receives any changes in the managers settings, so changing the sequence to the first set of Nimbus and then putting the color) should also work, but doesn’t even in jdk7

  UIManager.setLookAndFeel(info.getClassName()); UIManager.put("Panel.background", new Color(255,0,0)); //UIManager.put("control", Color.MAGENTA); 

It seems to be specific to Panel.background (and most likely to a group of others), the "control" in both jdks is fine, before and after installing LAF.

+2
source

Source: https://habr.com/ru/post/902494/


All Articles