Изменить цвет фона JComboBox Arrow - PullRequest
2 голосов
/ 03 января 2011

Я нашел следующее решение для изменения цвета стрелки JComboBox :

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

Как именно вы «рисуете иконку в графическом контексте BufferedImage»?

Ответы [ 2 ]

2 голосов
/ 03 января 2011

Как это:

    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);
        }
    }
1 голос
/ 03 января 2011

В качестве альтернативы рассмотрите возможность использования пользовательского экземпляра BasicArrowButton в ComboBoxUI, как показано в этом примере .

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...