Paul Reiners
Paul Reiners

Reputation: 7886

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 recursively 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"?

Upvotes: 2

Views: 4194

Answers (2)

trashgod
trashgod

Reputation: 205785

As an alternative, consider using a custom instance of BasicArrowButton in a ComboBoxUI, as shown in this example.

Upvotes: 1

Paul Reiners
Paul Reiners

Reputation: 7886

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);
        }
    }

Upvotes: 2

Related Questions