我可以修改现有对象的JComboBox弹出背景颜色吗? [英] Can I modify JComboBox popup background color of an existing object?

查看:129
本文介绍了我可以修改现有对象的JComboBox弹出背景颜色吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的JComboBox对象。我可以使用内部方法修改它的许多属性。但是,我找不到类似的方法来自定义弹出窗口的外观 - 特别是弹出窗口的背景颜色。我有一个现有的对象,所以我希望使用它现有的方法/属性,而不是写一个专用的类。这可能吗?

I have an existing JComboBox object. I can modify many of its properties using the internal methods. However, I could not find similar methods to customize the popup's appearance - specifically, the popup's background color. I have an existing object, so I wish to use its existing methods/properties, not to write a dedicated class. Is this possible?

注意:这个问题与上面的链接问题相同(错误地说明这个问题已经有了答案):那个问题询问了所选项目的bgcolor(在组合框的编辑框中);我问的是弹出框的bgcolor。

Note: this question is NOT the same as the linked question above (which incorrectly states that this question already has an answer): that question asked about the selected item's bgcolor (in the combobox's editbox); I am asking about the popup box's bgcolor.

推荐答案

正如eugener所说,使用自定义 ListCellRenderer 绝对是正确的方法。您只需要创建一个扩展 DefaultListCellRenderer 的类。此默认渲染器扩展 JLabel ,因此它更容易理解!你只需要调用 setBackground()

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);
    }
});

这篇关于我可以修改现有对象的JComboBox弹出背景颜色吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆