ListCellRenderer,所选值不可见
ListCellRenderer, selected value not visible
我有 JComboBox,我按如下方式应用 ListCellRenderer:
colorList = new JComboBox<>(COLORS_NAMES);
ColorComboBoxRenderer renderer = new ColorComboBoxRenderer(colorList);
renderer.setColors(COLORS);
renderer.setColorNames(COLORS_NAMES);
colorList.setRenderer(renderer);
它导致修改单元格,但我找不到为什么选择的值被记住但没有被描绘的原因。喜欢以下内容:
这是我的渲染器代码(省略了 setColors、getColors 等)
class ColorComboBoxRenderer extends JPanel implements ListCellRenderer{
JPanel textPanel;
JLabel text;
public ColorComboBoxRenderer(JComboBox combo){
textPanel = new JPanel();
textPanel.add(this);
text = new JLabel();
text.setOpaque(true);
text.setFont(combo.getFont());
textPanel.add(text);
}
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
boolean cellHasFocus) {
if (isSelected){
list.setSelectionBackground(colors[list.getSelectedIndex()]);
}
else{}
if(colors.length != colorNames.length){
System.out.println("colors.length doesn't match colorNames.length");
return this;
}
else if(colors == null){
System.out.println("Set colors by setColors first.");
return this;
}
else if(colorNames == null){
System.out.println("Set colorNames by setColorNames first.");
return this;
}
text.setText(" ");
if(index > -1){
text.setBackground(colors[index]);
text.setText(" ");
}
return text;
}
}
同样让我感到困惑的是,每次我将光标指向指定的单元格时,if(isSelected) block
都会完成,但我的直觉宁愿希望在 cellHasFocus
参数为真时发生这种情况。
提前致谢,因为我已经苦苦挣扎了 2 天;/
编辑 1
将 JComboBox 字段添加到 ColorComboBoxRenderer class,并在构造函数中对其进行初始化:
private JComboBox comboBox;
public ColorComboBoxRenderer(JComboBox combo) {
this.comboBox = combo;
//rest of code as it was
}
更改为:
if(isSelected){
list.setSelectionBackground(colors[list.getSelectedIndex()]);
}
收件人:
if (isSelected){
list.setSelectionBackground(colors[list.getSelectedIndex()]);
comboBox.setBackground(colors[list.getSelectedIndex()]);
}
结果:
现在效果更好了,所以你知道如何改变JComboBox背景但不影响下拉箭头吗?
在 EDIT 1 中,我们编辑 ComboBox 很重要,这是必要的,因为 ListRenderer 只生成下拉列表及其单元格,但不会影响 ComboBox 字段。所以我终于找到了必要的,是相当蛮力的方法。在 EDIT 1 中选择时,我更改了整个组合框背景,但随后返回了箭头状态,它位于 JComboBox 的索引 (0) 处。最后那段代码应该是这样的:
if (isSelected){
list.setSelectionBackground(colors[list.getSelectedIndex()]);
comboBox.setBackground(colors[list.getSelectedIndex()]);
comboBox.getComponent(0).setBackground(new JButton().getBackground());
}
现在一切正常:
我有 JComboBox,我按如下方式应用 ListCellRenderer:
colorList = new JComboBox<>(COLORS_NAMES);
ColorComboBoxRenderer renderer = new ColorComboBoxRenderer(colorList);
renderer.setColors(COLORS);
renderer.setColorNames(COLORS_NAMES);
colorList.setRenderer(renderer);
它导致修改单元格,但我找不到为什么选择的值被记住但没有被描绘的原因。喜欢以下内容:
这是我的渲染器代码(省略了 setColors、getColors 等)
class ColorComboBoxRenderer extends JPanel implements ListCellRenderer{
JPanel textPanel;
JLabel text;
public ColorComboBoxRenderer(JComboBox combo){
textPanel = new JPanel();
textPanel.add(this);
text = new JLabel();
text.setOpaque(true);
text.setFont(combo.getFont());
textPanel.add(text);
}
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
boolean cellHasFocus) {
if (isSelected){
list.setSelectionBackground(colors[list.getSelectedIndex()]);
}
else{}
if(colors.length != colorNames.length){
System.out.println("colors.length doesn't match colorNames.length");
return this;
}
else if(colors == null){
System.out.println("Set colors by setColors first.");
return this;
}
else if(colorNames == null){
System.out.println("Set colorNames by setColorNames first.");
return this;
}
text.setText(" ");
if(index > -1){
text.setBackground(colors[index]);
text.setText(" ");
}
return text;
}
}
同样让我感到困惑的是,每次我将光标指向指定的单元格时,if(isSelected) block
都会完成,但我的直觉宁愿希望在 cellHasFocus
参数为真时发生这种情况。
提前致谢,因为我已经苦苦挣扎了 2 天;/
编辑 1
将 JComboBox 字段添加到 ColorComboBoxRenderer class,并在构造函数中对其进行初始化:
private JComboBox comboBox;
public ColorComboBoxRenderer(JComboBox combo) {
this.comboBox = combo;
//rest of code as it was
}
更改为:
if(isSelected){
list.setSelectionBackground(colors[list.getSelectedIndex()]);
}
收件人:
if (isSelected){
list.setSelectionBackground(colors[list.getSelectedIndex()]);
comboBox.setBackground(colors[list.getSelectedIndex()]);
}
结果:
现在效果更好了,所以你知道如何改变JComboBox背景但不影响下拉箭头吗?
在 EDIT 1 中,我们编辑 ComboBox 很重要,这是必要的,因为 ListRenderer 只生成下拉列表及其单元格,但不会影响 ComboBox 字段。所以我终于找到了必要的,是相当蛮力的方法。在 EDIT 1 中选择时,我更改了整个组合框背景,但随后返回了箭头状态,它位于 JComboBox 的索引 (0) 处。最后那段代码应该是这样的:
if (isSelected){
list.setSelectionBackground(colors[list.getSelectedIndex()]);
comboBox.setBackground(colors[list.getSelectedIndex()]);
comboBox.getComponent(0).setBackground(new JButton().getBackground());
}
现在一切正常: