我试图找到未被选中的细胞使用的颜色。这通常是白色的,但是当我调用UIManager.getColor(“List.background”)时,它看起来与JPanels使用的颜色相同。当我调用新的JList()。getBackground()时,我得到了相同的可怕灰色,但是当我实际使用该列表时,它是白色的。如何从JList或UIManager获得此白色?我目前正在做的是找到背景颜色:
String[] contents = {"Foo", "Bar"};
JList list = new JList(contents);
// Prints true
System.out.println(list.getBackground().equals(new Color(237, 236, 235)));
由于List.selectionBackground给了我蓝色,我希望在单击一个单元格时看到它,我认为List.background会给我一个未选中单元格的颜色。什么是List.background实际返回一个值呢?
相关说明,是否列出了这些键的含义?我找到了related question,但没有一个答案提供了密钥的描述。
编辑:看来这是执行此操作的正确方法。但是,至少在GNOME中,调用setLookAndFeel时会出现问题。
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
String[] contents = {"Foo", "Bar"};
JList list = new JList(contents);
// Prints true
System.out.println(list.getBackground().equals(new Color(237, 236, 235)));
// Add list to a pane and display it, and it will actually be white
看起来这可能是一个错误,抱歉。
答案 0 :(得分:0)
SwingUtilities.updateComponentTreeUI(myFrame);
编辑:
嗯,你能澄清我(我们)你的问题是基于这个例子(死于Old.Good.Forum.Sun.com,:-)而且我知道Jeanette不会抗拒)注意:更好的方法是使用getListCellRendererComponent
EDIT2:请使用该检查camickr UI Defaluts
进行另一场比赛import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class JListDisabledItemDemo implements ItemListener, Runnable {
private static final String ITEMS[] = {"Black", "Blue", "Green", "Orange", "Purple", "Red", "White", "Yellow"};
private JList jList;
private JCheckBox[] checkBoxes;
private boolean[] enabledFlags;
@Override
public void run() {
JPanel pnlEnablers = new JPanel(new GridLayout(0, 1));
pnlEnablers.setBorder(BorderFactory.createTitledBorder("Enabled Items"));
checkBoxes = new JCheckBox[ITEMS.length];
enabledFlags = new boolean[ITEMS.length];
for (int i = 0; i < ITEMS.length; i++) {
checkBoxes[i] = new JCheckBox(ITEMS[i]);
checkBoxes[i].setSelected(true);
checkBoxes[i].addItemListener(this);
enabledFlags[i] = true;
pnlEnablers.add(checkBoxes[i]);
}
jList = new JList(ITEMS);
jList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jList.setSelectionModel(new DisabledItemSelectionModel());
jList.setCellRenderer(new DisabledItemListCellRenderer());
jList.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
System.out.println("selection");
}
}
});
JScrollPane scroll = new JScrollPane(jList);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
JFrame f = new JFrame("Colors");
Container contentPane = f.getContentPane();
contentPane.setLayout(new GridLayout(1, 2));
contentPane.add(pnlEnablers);
contentPane.add(scroll);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(240, 280);
f.setLocationRelativeTo(null);
UIManager.put("List.background", Color.red);
UIManager.put("List.selectionBackground", Color.orange);
UIManager.put("List.selectionForeground", Color.blue);
UIManager.put("Label.disabledForeground", Color.magenta);
SwingUtilities.updateComponentTreeUI(f);
f.setVisible(true);
}
@Override
public void itemStateChanged(ItemEvent event) {
JCheckBox checkBox = (JCheckBox) event.getSource();
int index = -1;
for (int i = 0; i < ITEMS.length; i++) {
if (ITEMS[i].equals(checkBox.getText())) {
index = i;
break;
}
}
if (index != -1) {
enabledFlags[index] = checkBox.isSelected();
jList.repaint();
}
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new JListDisabledItemDemo());
}
private class DisabledItemListCellRenderer extends DefaultListCellRenderer {
private static final long serialVersionUID = 1L;
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
if (enabledFlags[index]) {
return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
}
Component comp = super.getListCellRendererComponent(list, value, index, false, false);
comp.setEnabled(false);
return comp;
}
}
private class DisabledItemSelectionModel extends DefaultListSelectionModel {
private static final long serialVersionUID = 1L;
/**
* No need to override addSelectionInterval(int index0, int index1)
* since we're using SINGLE_SELECTION mode for this demo.
*/
@Override
public void setSelectionInterval(int index0, int index1) {
if (enabledFlags[index0]) {
super.setSelectionInterval(index0, index0);
} else {
/*
* The previously selected index is before this one,
* so walk forward to find the next selectable item.
*/
if (getAnchorSelectionIndex() < index0) {
for (int i = index0; i < enabledFlags.length; i++) {
if (enabledFlags[i]) {
super.setSelectionInterval(i, i);
return;
}
}
} /*
* Otherwise, walk backward to find the next selectable item.
*/ else {
for (int i = index0; i >= 0; i--) {
if (enabledFlags[i]) {
super.setSelectionInterval(i, i);
return;
}
}
}
}
}
}
}