我有一个JComboBox包含以下iems
{“select”,“one”,“two”}
我需要第一个项目的单独背景,所以, 我在它的渲染器中创造了一个条件,如
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (value != null){
Item item = (Item)value;
setText(item.getDescription());
}
if(index==-1){
setBackground(new Color(199,209,210));
setForeground(Color.BLACK);
}
return this;
}
}
所以我的问题是如果我们禁用JComboBox,我必须将组件背景颜色的第-1个索引设置为其他颜色
喜欢
if(index==-1){
setBackground(Color.RED);
}
请咨询
答案 0 :(得分:2)
最简单的方法总是最好的。由于您将渲染器分配给组合框,为什么不将组合框传递给组合框呢?只需创建一个包含对组合框的引用的自定义渲染器,然后使用getListCellRendererComponent
方法中存储的引用
答案 1 :(得分:0)
也许我在这里遗漏了一些东西,但是如果JComboBox
被禁用,则意味着它无法弹出其项目列表。但如果它没有显示其项目列表,那么这意味着它没有使用该列表的渲染器。
那么,为什么需要在渲染器中获得对组合框的引用?