如何更改不可编辑jcombobox中选定值的颜色?更改不可编辑jcombobox中选定值的颜色
我知道我必须使用自定义渲染器来更改出现在组合框下拉列表中的项目的颜色。我也知道setForeground()方法可用于组合框的编辑器组件,但仅适用于可编辑组合框。我也知道我可以在UIManager课堂上玩耍,但是这会影响到全球的这些属性。
但是,这些不是我所需要的。此图片
显示我想影响的部分。
如何更改不可编辑jcombobox中选定值的颜色?更改不可编辑jcombobox中选定值的颜色
我知道我必须使用自定义渲染器来更改出现在组合框下拉列表中的项目的颜色。我也知道setForeground()方法可用于组合框的编辑器组件,但仅适用于可编辑组合框。我也知道我可以在UIManager课堂上玩耍,但是这会影响到全球的这些属性。
但是,这些不是我所需要的。此图片
显示我想影响的部分。
增加ComboBox的编辑
得到组合框的编辑器组件
的editorComponent设置为可聚焦(假)
不知道这是你以后,你仍然可以更改所选项目,但无法专注于editorComponent更改项目。
如何更改不可编辑的 jcombobox中所选值的颜色?对于
UIManager
这里
- 变化值是可编辑的基本解决方法(以去除背景 当前背景,公告不存在快捷方式MetalComboBoxButton, 则外观和感觉敏感,适用于金属和 仅限物质L & f)
import java.awt.*;
import java.util.Vector;
import javax.swing.*;
import javax.swing.UIManager;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.metal.MetalComboBoxButton;
public class MyComboBox {
private Vector<String> listSomeString = new Vector<String>();
private JComboBox someComboBox = new JComboBox(listSomeString);
private JComboBox editableComboBox = new JComboBox(listSomeString);
private JComboBox non_EditableComboBox = new JComboBox(listSomeString);
private JFrame frame;
public MyComboBox() {
//for (int i = 0; i < 100000; i++) {
listSomeString.add("-");
listSomeString.add("Snowboarding");
listSomeString.add("Rowing");
listSomeString.add("Knitting");
listSomeString.add("Speed reading");
//}
//
someComboBox.setPrototypeDisplayValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
someComboBox.setFont(new Font("Serif", Font.BOLD, 16));
someComboBox.setEditable(true);
someComboBox.getEditor().getEditorComponent().setBackground(Color.YELLOW);
((JTextField) someComboBox.getEditor().getEditorComponent()).setBackground(Color.YELLOW);
//
editableComboBox.setPrototypeDisplayValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
editableComboBox.setFont(new Font("Serif", Font.BOLD, 16));
editableComboBox.setEditable(true);
JTextField text = ((JTextField) editableComboBox.getEditor().getEditorComponent());
text.setBackground(Color.YELLOW);
JComboBox coloredArrowsCombo = editableComboBox;
Component[] comp = coloredArrowsCombo.getComponents();
for (int i = 0; i < comp.length; i++) {// hack valid only for Metal L&F
if (comp[i] instanceof MetalComboBoxButton) {
MetalComboBoxButton coloredArrowsButton = (MetalComboBoxButton) comp[i];
coloredArrowsButton.setBackground(null);
break;
}
}
//
non_EditableComboBox.setPrototypeDisplayValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
non_EditableComboBox.setFont(new Font("Serif", Font.BOLD, 16));
//
frame = new JFrame();
frame.setLayout(new GridLayout(0, 1, 10, 10));
frame.add(someComboBox);
frame.add(editableComboBox);
frame.add(non_EditableComboBox);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(100, 100);
frame.pack();
frame.setVisible(true);
//System.out.println(listSomeString.size());
//System.out.println(someComboBox.getItemCount());
//System.out.println(editableComboBox.getItemCount());
//System.out.println(non_EditableComboBox.getItemCount());
}
public static void main(String[] args) {
UIManager.put("ComboBox.background", new ColorUIResource(Color.yellow));
UIManager.put("JTextField.background", new ColorUIResource(Color.yellow));
UIManager.put("ComboBox.selectionBackground", new ColorUIResource(Color.magenta));
UIManager.put("ComboBox.selectionForeground", new ColorUIResource(Color.blue));
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
MyComboBox aCTF = new MyComboBox();
}
});
}
}
我的问题是,这个解决方案只改变了不可编辑的组合框的选定值的颜色,如果它真的被选中(选择一个值后),如果它失去了焦点,它的颜色变回。此外,这会影响所有组合框,我只想更改特定组合的颜色。 –
@JózsefLegoza不清楚你的评论,必须详细阐述一点,需要在屏幕上看到,选择哪个项目,不要混淆用户.... – mKorbel
我想设置文本颜色的选定的值(通过选择我指的是下拉箭头左侧的部分)除黑色以外的任何值。如果我用户UIManger,所有组合框将具有相同的属性,但我只想改变特定组合框的颜色。在您的示例中尝试以下操作:在第三个组合框中选择一个值,然后单击其他组合框之一。第三个组合框的文本颜色从蓝色切换回黑色。在这种情况下,我希望文本颜色保持蓝色。 –
我想改变颜色未做组合框编辑。 –
@Michael Dunn然后被改变按钮背景也是可编辑的JComboBox – mKorbel