I'm trying to customize the look of my JComboBox using nimbus L&F.
Here is some code:
NamedPainter.java
package gui.combo;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import javax.swing.JComponent;
import javax.swing.Painter;
public class NamedPainter implements Painter<JComponent>
{
String name;
public NamedPainter(String name)
{
this.name = name;
}
@Override
public void paint(Graphics2D g, JComponent object, int width, int height)
{
g.setColor(Color.LIGHT_GRAY);
g.fillRect(0, 0, width, height);
g.setFont(new Font("Arial", Font.PLAIN, 12));
g.setColor(Color.YELLOW);
g.drawString(name, 0, 10);
}
}
ColorRectanglePainter.java
package gui.combo;
import java.awt.Color;
import java.awt.Graphics2D;
import javax.swing.JComponent;
import javax.swing.Painter;
public class ColorRectanglePainter implements Painter<JComponent>
{
private final Color color;
public ColorRectanglePainter(final Color color)
{
this.color = color;
}
@Override
public void paint(Graphics2D g, JComponent object, int width, int height)
{
g.setColor(color);
g.fillRect(0, 0, width, height);
}
}
CustomizeComboNimbus.java
package gui.combo;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.util.Map.Entry;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
public class CustomizeComboNimbus
{
/**
* @param args
*/
public static void main(String[] args)
{
// Set nimbus L&F
try
{
for(LookAndFeelInfo info : UIManager.getInstalledLookAndFeels())
{
if("Nimbus".equals(info.getName()))
{
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
}
catch(Exception e)
{
// No nimbus...
e.printStackTrace();
return;
}
ColorRectanglePainter redPainter = new ColorRectanglePainter(Color.RED);
// Get default UI and modify it
final UIDefaults boxDefaults = new UIDefaults();
for(Entry<Object, Object> entry : UIManager.getLookAndFeelDefaults().entrySet())
{
try
{
String key = (String)entry.getKey();
if(key.startsWith("ComboBox"))
{
if(key.contains("Painter"))
{
if(key.contains("arrowButton"))
{
// Set a painter which paint a red rectangle for arrowButton
boxDefaults.put(key, redPainter);
System.err.println("Replacing the painter for " + key + " with redPainter");
}
else
{
// Set a painter that display the name of the nimbusKey when it is triggered
NamedPainter painter = new NamedPainter(key);
boxDefaults.put(key, painter);
System.err.println("Replacing the painter for " + key + " with NamedPainter");
}
}
}
}
catch(Exception e)
{
}
}
final String[] toDisplay = { "Hello", "World", "Pimp", "My", "Combo" };
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
JFrame frame = new JFrame("Pimp my combo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComboBox<String> classicCombo = new JComboBox<>(toDisplay);
JComboBox<String> pimpedCombo = new JComboBox<>(toDisplay);
// set the modif to pimpedCombo
pimpedCombo.putClientProperty("Nimbus.Overrides", boxDefaults);
pimpedCombo.putClientProperty("Nimbus.Overrides.InheritDefaults", false);
Container pane = frame.getContentPane();
pane.setLayout(new GridLayout(2, 1));
pane.add(classicCombo);
pane.add(pimpedCombo);
frame.pack();
frame.setVisible(true);
}
});
}
}
Here is the console output:
Replacing the painter for ComboBox:"ComboBox.arrowButton" Editable+Selected].backgroundPainter with redPainter
Replacing the painter for ComboBox:"ComboBox.textField"[Enabled].backgroundPainter with NamedPainter
Replacing the painter for ComboBox[Pressed].backgroundPainter with NamedPainter
Replacing the painter for ComboBox[Editable+Focused].backgroundPainter with NamedPainter
Replacing the painter for ComboBox:"ComboBox.arrowButton"[Disabled].foregroundPainter with redPainter
Replacing the painter for ComboBox:"ComboBox.arrowButton"[Editable+MouseOver].backgroundPainter with redPainter
Replacing the painter for ComboBox[Enabled+Selected].backgroundPainter with NamedPainter
Replacing the painter for ComboBox[MouseOver].backgroundPainter with NamedPainter
Replacing the painter for ComboBox:"ComboBox.arrowButton"[Selected].foregroundPainter with redPainter
Replacing the painter for ComboBox:"ComboBox.arrowButton"[Disabled+Editable].backgroundPainter with redPainter
Replacing the painter for ComboBox[Editable+Pressed].backgroundPainter with NamedPainter
Replacing the painter for ComboBox[Focused+Pressed].backgroundPainter with NamedPainter
Replacing the painter for ComboBox[Disabled+Pressed].backgroundPainter with NamedPainter
Replacing the painter for ComboBox:"ComboBox.arrowButton"[Editable+Enabled].backgroundPainter with redPainter
Replacing the painter for ComboBox:"ComboBox.arrowButton"[Enabled].foregroundPainter with redPainter
Replacing the painter for ComboBox[Disabled+Editable].backgroundPainter with NamedPainter
Replacing the painter for ComboBox:"ComboBox.arrowButton"[MouseOver].foregroundPainter with redPainter
Replacing the painter for ComboBox[Disabled].backgroundPainter with NamedPainter
Replacing the painter for ComboBox:"ComboBox.textField"[Disabled].backgroundPainter with NamedPainter
Replacing the painter for ComboBox[Enabled].backgroundPainter with NamedPainter
Replacing the painter for ComboBox[Editable+MouseOver].backgroundPainter with NamedPainter
Replacing the painter for ComboBox:"ComboBox.textField"[Selected].backgroundPainter with NamedPainter
Replacing the painter for ComboBox[Focused].backgroundPainter with NamedPainter
Replacing the painter for ComboBox:"ComboBox.arrowButton"[Editable+Pressed].backgroundPainter with redPainter
Replacing the painter for ComboBox:"ComboBox.arrowButton"[Pressed].foregroundPainter with redPainter
Replacing the painter for ComboBox[Editable+Enabled].backgroundPainter with NamedPainter
Replacing the painter for ComboBox[Focused+MouseOver].backgroundPainter with NamedPainter
And of course, what it looks like:
However, this is not exactly what I expected! I thought that having replaced all the ComboBox:arrowButtonPainter with the custom redPainter
, I would not see the little triangular black (or white) arrow but just a red rectangle.
Also, I did not manager to change the color of the foreground text. How can I do that, both in the combo and in the selection popup menu?
[EDIT]
Further investigations: I tried to put the properties using UIManager.getLookAndFeelDefaults().put
instead of boxDefaults.put
, and I got the expected result for the arrowButton, which appears as a red square (for both classic and pimped combo, obviously). So, I guess what I'm doing wrong is to override the properties for the pimped combo, i.e. thos two lines that I got from Jasper Pott's blog
pimpedCombo.putClientProperty("Nimbus.Overrides", boxDefaults);
pimpedCombo.putClientProperty("Nimbus.Overrides.InheritDefaults", false);
Can anyone helps on that?
[EDIT 2]
I also note inconsistent behaviour if I use UIManager.put
instead of UIManager.getLookAndFeelDefaults().put
, where the arrowButton will appear red for example only on mouseOver, or cliked, etc. Javadoc says UIManger.put
only affects the "developers defaults", not the L&F defaults. What's the difference? Any help, link to good documentation on how everything works would be helpful.