开发者

Remove the down arrow in JComboBox

开发者 https://www.devze.com 2023-03-31 13:41 出处:网络
I want to create an auto complete program and I am using JComboBox for this. Now I want to remove the down arrow in the JComboBox. How to rem开发者_如何学Pythonove the arrow?JComboBox is compound JCo

I want to create an auto complete program and I am using JComboBox for this.

Now I want to remove the down arrow in the JComboBox. How to rem开发者_如何学Pythonove the arrow?


JComboBox is compound JComponent and contains JButton with Icon, you can remove that by setIcon(null) or replace with another Icon

for example (how to do it with simple steps, NOTICE valid just for Metal Look and Feel)

        JComboBox coloredArrowsCombo = myComboBox;
        BufferedImage coloredArrowsImage = null;
        try {
            coloredArrowsImage = ImageIO.read(AppVariables.class.getResource("resources/passed.png"));
        } catch (IOException ex) {
            Logger.getLogger(someClessName.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (!(coloredArrowsImage == null)) {
            Icon coloredArrowsIcon = new ImageIcon(coloredArrowsImage);
            Component[] comp = coloredArrowsCombo.getComponents();
            for (int i = 0; i < comp.length; i++) {
                if (comp[i] instanceof MetalComboBoxButton) {
                    MetalComboBoxButton coloredArrowsButton = (MetalComboBoxButton) comp[i];
                    coloredArrowsButton.setComboIcon(coloredArrowsIcon);
                    break;
                }
            }
        }

EDIT: for better output you can put here coloredArrowsButton.setRolloverIcon(someIcon);

0

精彩评论

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