Java JComboBox当前所选项目背景和Windows LaF
我当时找不到其他办法:Java JComboBox当前所选项目背景和Windows LaF,java,swing,colors,jcombobox,look-and-feel,Java,Swing,Colors,Jcombobox,Look And Feel,我当时找不到其他办法: UIManager.put("ComboBox.background",new Color(31,0,104)); UIManager.put("ComboBox.selectionBackground",new Color(31,0,104)); 设置JComboBox中当前选定项目的背景颜色。 不幸的是,这对窗口中我不想要的所有组合框都是一样的 这就是它目前的工作方式: 这就是我希望它的行为: 我尝试创建自定义的Defau
UIManager.put("ComboBox.background",new Color(31,0,104));
UIManager.put("ComboBox.selectionBackground",new Color(31,0,104));
设置JComboBox中当前选定项目的背景颜色。
不幸的是,这对窗口中我不想要的所有组合框都是一样的
这就是它目前的工作方式:
这就是我希望它的行为:
我尝试创建自定义的DefaultListCellRenderer并在那里调用setBackground,但没有任何帮助。我还尝试重写BasicComboxUI,但我可能不知道应该重写哪个方法才能获得结果
编辑:
根据建议,这是我代码的一小部分,但没有帮助
public void colorizeComboBox(){
jComboBox1.setRenderer(new ColourListCellRenderer());
jComboBox1.setUI(new BasicComboBoxUI(){
@Override
protected JButton createArrowButton() {
JButton b = this.createCustomArrowButton();
b.setContentAreaFilled(false);
b.setBackground(new Color(31,0,104));
b.setBorder(BorderFactory.createEmptyBorder());
return b;
}
public JButton createCustomArrowButton() {
JButton b=new JButton(new ImageIcon(getClass().getResource("/server/Pictures/arrowdown.png")));
b.setBorder(new EmptyBorder(0, 0, 0, 0));
b.setOpaque(true);
return b;
}
});
和自定义渲染器:
public static class ColourListCellRenderer extends DefaultListCellRenderer {
@Override
public Color getBackground() {
return new Color(31,0,104);
}
}
但现在所有这些更改都是当前选定项的背景为白色。覆盖DefaultListCellRenderer的getBackground和GetListCellRenderComponent方法
public class ColoredListCellRenderer extends DefaultListCellRenderer {
private Color selectedColor;
private Color backgroundColor;
private Color lastColor;
public ColoredListCellRenderer(Color backgroundColor, Color selectedColor) {
this.selectedColor = selectedColor;
this.backgroundColor = backgroundColor;
lastColor = backgroundColor;
}
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (isSelected) {
lastColor = selectedColor;
} else {
lastColor = backgroundColor;
}
c.setBackground(lastColor);
return c;
}
@Override
public Color getBackground() {
return lastColor;
}
}
OP的答案从问题移到了答案: 确定最终找到解决方案: 我必须覆盖BasicComboxUI中的paintCurrentValueBackground(..) ,然后将此UI设置为组合框,但focus仍在制造问题和 将组合框X绘制为灰色,因此我禁用了对该组合框的关注
请解释你想要什么。是否要使选定的行看起来像未选定的行?调用时,当项目呈现“选定”值时,
ListCellRenderer
将被传递给-1
(我认为)项索引
值。出现这种情况时,您可以尝试执行一些特殊的渲染…我尝试了您的代码,但它没有任何颜色。我尝试了您的解决方案,但它似乎只是将所选项目的背景更改为白色。似乎是我编辑的帖子
jComboBox1.setUI(new BasicComboBoxUI(){
@Override
public void paintCurrentValueBackground(Graphics g,Rectangle bounds,boolean hasFocus){
Color t = g.getColor();
if ( comboBox.isEnabled() )
g.setColor(new Color(31,0,104));
else
g.setColor(DefaultLookup.getColor(comboBox, this,
"ComboBox.disabledBackground", null));
g.fillRect(bounds.x,bounds.y,bounds.width,bounds.height);
g.setColor(t);
}
});
jComboBox1.setFocusable(false);