Question

I wrote my own ListCellRenderer (code below) which gets my cells transparent. Now if I run this, every single time I click on a listitem, it becomes more whiter than before. Could it be, that the JList always add's this new JLabel - that I created - and overlays the last one?

    jListPriority.setOpaque(true);
        jListPriority.setBackground(new Color(255, 255, 255, 10));
        jListPriority.setBorder(BorderFactory.createLineBorder(new Color(250, 250, 250)));

        jListPriority.setCellRenderer(new ListCellRenderer() {

                private JLabel label = new JLabel();

                public Component getListCellRendererComponent(JList list, Object value,   
                                 int index, boolean isSelected, boolean cellHasFocus) {

                    label.setOpaque(true);
                    label.setText(value.toString());
                    label.setForeground(new Color(250, 250, 250));
                    label.setFont(new Font("Lucida Grande", Font.PLAIN, 13));
                    label.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(250, 250, 250, 50)));
                    label.setPreferredSize(new Dimension(label.getPreferredSize().width, label.getPreferredSize().height + 3));

                    if (isSelected) {
                        label.setBackground(new Color(255, 255, 255, 20));
                    } else {
                        label.setBackground(new Color(255, 255, 255, 10));
                    }

                    return label;
                }
            });
Was it helpful?

Solution

The default composite for Graphics2D uses the AlphaComposite.SRC_OVER rule, so the result is expected. You'll need to specify a suitable mode for your desired effect. You may get some ideas from this example.

image

OTHER TIPS

I think you dont want to create too many jlabel instances. In your example you create one instance of jlabel per one invocation of get... method Just convert variable label into field.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top