JComboBox 每个项目的颜色不同失败
- 目的:有一个具有不同背景颜色和文本的JComboBox 在每个项目中。
- 我的问题:背景颜色没有改变,文本也不是我在setText中设置的,在System.out.println中已正确显示。 getSelectedIndex() 效果很好。
捕获: https://i.sstatic.net/EgfZs.png
以下是代码在我消化并试错 Dr.Google 显示的内容之后:
public class ColorCode{
private Color color;
private String alias;
...
}
public class ElectronicColorCode extends JFrame implements ActionListener{
private JComboBox[] selections = new JComboBox[4];
...
public ElectronicColorCode(){
for(int i=0; i<selections.length; i++){
selections[i] = new JComboBox();
for(int j=0; j<tolColorSets.length; j++)
selections[i].addItem(new ComboBoxRenderer(colorSets[j]));
}
selections[i].addActionListener(this);
...
}
}
class ComboBoxRenderer extends JLabel implements ListCellRenderer{
private ColorCode colorCode;
public ComboBoxRenderer(ColorCode colorCode){
super();
this.colorCode = colorCode;
setBackground(colorCode.getColor());
setText(colorCode.getAlias());
System.out.println(colorCode.getAlias());
}
public Component getListCellRendererComponent(JList list, Object obj, int row, boolean isSelected, boolean hasFocus){
return this;
}
}
- Purpose: to have a JComboBox with different background colors and text
in each item. - My problem: The background color doesn't change, and the text is not what I've set in setText, which have been correctly shown in System.out.println. The getSelectedIndex() works well.
The capture: https://i.sstatic.net/EgfZs.png
The following is the code after I've digested and try-and-error what Dr.Google shows:
public class ColorCode{
private Color color;
private String alias;
...
}
public class ElectronicColorCode extends JFrame implements ActionListener{
private JComboBox[] selections = new JComboBox[4];
...
public ElectronicColorCode(){
for(int i=0; i<selections.length; i++){
selections[i] = new JComboBox();
for(int j=0; j<tolColorSets.length; j++)
selections[i].addItem(new ComboBoxRenderer(colorSets[j]));
}
selections[i].addActionListener(this);
...
}
}
class ComboBoxRenderer extends JLabel implements ListCellRenderer{
private ColorCode colorCode;
public ComboBoxRenderer(ColorCode colorCode){
super();
this.colorCode = colorCode;
setBackground(colorCode.getColor());
setText(colorCode.getAlias());
System.out.println(colorCode.getAlias());
}
public Component getListCellRendererComponent(JList list, Object obj, int row, boolean isSelected, boolean hasFocus){
return this;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不将渲染器添加为组合框的项目。渲染器用于渲染存储在模型中的对象。如果需要,可以将自定义对象添加到模型中,其中包含要在渲染器中显示的文本和背景颜色。
这是一个简单的示例,展示了如何执行此操作。显然,您需要自定义代码来存储和渲染背景颜色而不是 id。
You don't add renderers as an item for the combo box. The renderer is used to renderer the Object that is stored in the model. If you want you can add a custom object to the model that contains both the text and the background color you want displayed in the renderer.
Here is a simple example that shows how to do this. You will obviously need to customize the code to store and render the background color instead of the id.