Can I modify JComboBox popup background color of an existing object?

让人想犯罪 __ 提交于 2019-12-05 10:21:25

As eugener said, using a custom ListCellRenderer is definitely the right way to do this. You just need to create a class that extends DefaultListCellRenderer. This default renderer extends JLabel so it couldn't be easier to understand! You just need to make a call to setBackground().

JComboBox combo = new JComboBox(new String[] { "A", "B", "C", "D" });
combo.setRenderer(new DefaultListCellRenderer() {
    public void paint(Graphics g) {
        setBackground(Color.YELLOW);
        setForeground(Color.RED);
        super.paint(g);
    }
});

You'd have to create a custom comboxbox renderer. More Information is here: http://download.oracle.com/javase/tutorial/uiswing/components/combobox.html#renderer

Have you tried:

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