更改JComboBox颜色WITHOUT渲染器 [英] Change JComboBox colours WITHOUT renderer
问题描述
我可以使用以下方式更改ComboBox背景颜色:
I can change the ComboBox background color using:
UIManager.put("ComboBox.background", Color.RED);
。
更改 [selected] .background ,请查看 Nimbus Defaults 的属性被称为 ComboBox:ComboBox.listRenderer[Selected] .background ,所以我试过:
But to change the [selected].background, having a look at Nimbus Defaults the property is called ComboBox:"ComboBox.listRenderer"[Selected].background, so I tried with:
UIManager.put("ComboBox:\"ComboBox.listRenderer\"[Selected].background", Color.RED);
但不起作用。
我想做一个渲染器(我试过,并给许多问题到一个长的代码,我甚至没有写自己,如果我这样做,渲染组合框到JFileChoosers是一个额外的问题)。那么,有没有任何解决方案来解决这个使用UIMAnager.put()?
I want to do this with a renderer (which I have tried and gives many problems into a long code I even hadn't written myself, and rendering the comboboxes into the JFileChoosers is an extra problem if I go that way). So, is there any solution to fix this using UIMAnager.put()?
推荐答案
设置不同颜色
,不使用 Nimbus defaluts
1 /用于单独的JComboBox
1/ for separate JComboBox
((JTextField) myJComboBox.getEditor().getEditorComponent())
#setBackground(Color.xxxx);
2 / for JFileChooser
2/ for JFileChooser
-
从
JFileChooser
中提取所有JComponents
(复合JComponents
)为自动此处,与
JList
和JScrooPane
extract all
JComponents
fromJFileChooser
(compoundJComponents
) as sugested here, same way as is described forJList
andJScrooPane
最安全的方式提取所有 JComponents
从 JFileChooser
http://stackoverflow.com/questions/6758719/uimanager-color-at-a-jfilechooser/6758985#6758985\">在这里
safiest way by extract all JComponents
from JFileChooser
as suggested in your previous post about that here
3 /使用 NimbusDefalut
为
-
JTextField
和我在添加1中建议的
JTextField
and as suggested in my add No.1
JList
,从 JTable
中选择的HighLighter p>
JComboBox's DropDown List
from defaluts for JList
, HighLighter for selection from JTable
编辑:
代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.basic.BasicComboPopup;
public class DisabledEditableCombo extends JFrame {
private static final long serialVersionUID = 1L;
private String comboList[] = (new String[]{"-", "London", "New York", "Sydney", "Tokyo"});
private JComboBox cmb = new JComboBox(comboList);
private JComboBox cmb1 = new JComboBox(comboList);
private JComboBox cmb2 = new JComboBox(comboList);
private JComboBox cmb3 = new JComboBox(comboList);
private JList list;
private JCheckBox checkBox = new JCheckBox("Combo enabled", false);
public DisabledEditableCombo() {
JLabel lbl = new JLabel("Editable JComboBox");
cmb.setEditable(true);
((JTextField) cmb.getEditor().getEditorComponent()).setDisabledTextColor(Color.red);
((JTextField) cmb.getEditor().getEditorComponent()).setBackground(Color.green);
cmb.setSelectedItem("Just Editable");
JLabel lbl1 = new JLabel("Non-Editable JComboBoxes");
//UIManager.put("ComboBox.disabledForeground", Color.red.darker().darker());
cmb1.setSelectedItem("Sydney");
cmb1.setRenderer(new DefaultListCellRenderer() {// ListCellRenderer
private static final long serialVersionUID = 1L;
@Override
public void paint(Graphics g) {
setBackground(cmb1.getBackground());
setForeground(Color.red);
super.paint(g);
}
});
cmb2.getEditor().getEditorComponent().setForeground(Color.blue);
((JTextField) cmb2.getEditor().getEditorComponent()).setDisabledTextColor(Color.red);
cmb2.setSelectedItem("London");
cmb3.setSelectedItem("Sydney");
checkBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
boolean selected = checkBox.isSelected();
cmb.setEnabled(selected);
cmb1.setEnabled(selected);
cmb2.setEnabled(selected);
cmb2.setEditable(!cmb2.isEnabled());
cmb2.setForeground(selected ? Color.blue : Color.red);
if (cmb2.getEditor() != null) {
((JTextField) cmb2.getEditor().getEditorComponent()).setDisabledTextColor(Color.red);
}
cmb3.setEnabled(selected);
Object child = cmb3.getAccessibleContext().getAccessibleChild(0);
BasicComboPopup popup = (BasicComboPopup) child;
list = popup.getList();
if (list != null) {
if (selected) {
list.setForeground(Color.blue);
} else {
list.setForeground(Color.red);
}
}
}
});
cmb.setEnabled(false);
cmb1.setEnabled(false);
cmb2.setEnabled(false);
cmb3.setEnabled(false);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(300, 300));
setLocation(150, 100);
setLayout(new GridLayout(7, 0, 10, 10));
add(lbl);
add(cmb);
add(lbl1);
add(cmb1);
add(cmb2);
add(checkBox);
add(cmb3);
pack();
setVisible(true);
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
DisabledEditableCombo disabledEditableCombo = new DisabledEditableCombo();
}
});
}
}
这篇关于更改JComboBox颜色WITHOUT渲染器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!