How can I change the arrow style in a JComboBox

折月煮酒 提交于 2019-11-26 05:36:30

问题


Let\'s say I want to use a custom image for the arrow in JComboBox, how can I do this?

I understand it\'s possible using the synth xml files, or maybe even UIManager.put(...), but I don\'t know how. All I want to do at this time is change the arrow image to something else, either programatically or even just overriding the image it uses. How exactly can I do this?


回答1:


You can override createArrowButton() in BasicComboBoxUI. BasicArrowButton is a convenient starting point.

class ColorArrowUI extends BasicComboBoxUI {

    public static ComboBoxUI createUI(JComponent c) {
        return new ColorArrowUI();
    }

    @Override protected JButton createArrowButton() {
        return new BasicArrowButton(
            BasicArrowButton.SOUTH,
            Color.cyan, Color.magenta,
            Color.yellow, Color.blue);
    }
}

Then install it.

JComboBox combo = new JComboBox();
combo.setUI(ColorArrowUI.createUI(combo));


来源:https://stackoverflow.com/questions/3008447/how-can-i-change-the-arrow-style-in-a-jcombobox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!