开发者

Customizing JFileChooser: FileFilters lost

I finaly customized selection colors at JList a JComboBoxes at my JFileChooser using this method that Eng. Fouad suggested here

public void customizeJFileChooser(Conta开发者_运维技巧iner 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);
            }
        }
    }

works great for the colors but... now I have a problem with the FileFilter names, as you can see above:

Customizing JFileChooser: FileFilters lost

If I don't call the customizeJFileChooser it gets the names right, so it must be a problem with that method. Any help?


Most likely the ListCellRenderer is not simply a DefaultListCellRenderer, but a derived class. So, the solution is to obtain the original and wrap it, rather than replace it.

        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;
                }
            });
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜