0

I finaly customized selection colors at JList a JComboBoxes at my JFileChooser using this method that Eng. Fouad suggested 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);
            }
        }
    }

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

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

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

Community
  • 1
  • 1
Roman Rdgz
  • 12,836
  • 41
  • 131
  • 207

1 Answers1

2

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;
                }
            });
        }
Devon_C_Miller
  • 16,248
  • 3
  • 45
  • 71