开发者

Change JComboBox Arrow's background color

开发者 https://www.devze.com 2023-02-02 00:36 出处:网络
I found the following solution for changing a JComboBox arrow\'s color: For JComboBox and Metal L&F

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消