views:

139

answers:

0

I have the following Java Swing code that shows a JList with custom cell renderer (JCheckBox used as renderer component).

The JList itself is made transparent, and also the JCheckBox is made transparent, so basically the color of the background container (yellow) should shine through. This works fine on Windows and on Linux, but on MacOS the JCheckBox cell renderer is not made transparent. Rather it shows with its configured background color (blue in this case).

package dummy;

import javax.swing.*;
import java.awt.*;

public class App 
{
    private static class CheckRenderer implements ListCellRenderer {
        private JCheckBox chk;

        public CheckRenderer() {
            chk = new JCheckBox("hello");
            chk.setOpaque(false);
            chk.setBackground(Color.blue);
        }

        public Component getListCellRendererComponent(JList list, Object value,
                           int index, boolean isSelected, boolean cellHasFocus)
            chk.setText(value.toString());
            return chk;
        }
    }

    public static void main( String[] args )
    {
        JFrame frame = new JFrame();
        JPanel container = new JPanel();
        container.setBackground(Color.yellow);
        frame.getContentPane().add(container);

        JList lst = new JList(new String[] { "one", "two", "three" });
        lst.setOpaque(false);
        lst.setBackground(Color.green);
        lst.setCellRenderer(new CheckRenderer());
        lst.setPreferredSize(new Dimension(100, 150));
        lst.setBorder(BorderFactory.createLineBorder(Color.black));
        container.add(lst);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(320, 240);
        frame.setVisible(true);
    }
} 

If I set the background color on the JCheckBox to null, then it displays with a green background, which means it takes the background color from the JList.

Anyone ever encountered this behavior? I have no idea how to work around this and make my JList transparent on all platforms.

Thanks a lot,

Gabi.