gpt4 book ai didi

java - 自定义JFileChooser : FileFilters lost

转载 作者:行者123 更新时间:2023-12-02 08:13:42 26 4
gpt4 key购买 nike

我最终使用 Eng. 的方法在 JFileChooser 的 JList 和 JComboBoxes 中自定义了选择颜色。福阿德建议here

public void customizeJFileChooser(Container c)
{
Component[] cmps = c.getComponents();
for (Component cmp : cmps)
{
if (cmp instanceof JList)
{
((JList)cmp).setSelectionBackground(new Color(164,164,164));
}
if (cmp instanceof JComboBox)
{
((JComboBox)cmp).setRenderer(new DefaultListCellRenderer() {
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
Component comp = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (isSelected)
comp.setBackground(new Color(164,164,164));
return comp;
}
});
}
if (cmp instanceof Container)
{
customizeJFileChooser((Container) cmp);
}
}
}

对于颜色效果很好,但是...现在我对 FileFilter 名称有问题,如您在上面看到的:

How it looks, and how I should look (and looked before changing the colors)

如果我不调用customizeJFileChooser,它就会得到正确的名称,所以它一定是该方法的问题。有什么帮助吗?

最佳答案

ListCellRenderer 很可能不仅仅是一个 DefaultListCellRenderer,而是一个派生类。因此,解决方案是获取原件并将其包装,而不是替换它。

        if (cmp instanceof JComboBox)
{
((JComboBox)cmp).setRenderer(new DefaultListCellRenderer() {
private ListCellRenderer superLCR = ((JComboBox)cmp).getRenderer();
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
Component comp = superLCR.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (isSelected)
comp.setBackground(new Color(164,164,164));
return comp;
}
});
}

关于java - 自定义JFileChooser : FileFilters lost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6858494/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com