Настройка JFileChooser: FileFilters потерян - PullRequest
0 голосов
/ 28 июля 2011

Я окончательно настроил выделенные цвета в JList a JComboBoxes в моем JFileChooser, используя этот метод, который Eng. Фуад предложил здесь

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, он получает правильные имена, поэтому это должно быть проблемой с этим методом. Любая помощь?

1 Ответ

2 голосов
/ 28 июля 2011

Скорее всего, 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;
                }
            });
        }
...