Change JComboBox Arrow's background color
I found the following solution for changing a JComboBox arrow's color:
For JComboBox and Metal L&F
-- iterate recursivel开发者_Go百科y over the components of the JComboBox and grab a reference
to the button of class javax.swing.plaf.metal.MetalComboBoxButton
-- get the icon by getComboIcon()
-- create a BufferedImage (type ARGB) the size of the icon
-- paintIcon the icon to the Graphics context of the BufferedImage
-- iterate over the pixels of the BufferedImage and change any non-zero pixels
(by getRGB) to the color you want (by setRGB).
-- construct a new ImageIcon from the image
-- set the new icon to the button by setComboIcon
How exactly do you "paintIcon the icon to the Graphics context of the BufferedImage"?
Like this:
int componentCount = comboBox.getComponentCount();
for (int i = 0; i < componentCount; i++) {
Component component = comboBox.getComponent(i);
if (component instanceof MetalComboBoxButton) {
MetalComboBoxButton metalComboBoxButton =
(MetalComboBoxButton) component;
Icon comboIcon = metalComboBoxButton.getComboIcon();
BufferedImage bufferedImage =
new BufferedImage(
comboIcon.getIconWidth(),
comboIcon.getIconHeight(),
BufferedImage.TYPE_INT_ARGB);
comboIcon.paintIcon(
metalComboBoxButton, bufferedImage.getGraphics(), 0, 0);
}
}
As an alternative, consider using a custom instance of BasicArrowButton
in a ComboBoxUI
, as shown in this example.
精彩评论