Markus
Markus

Reputation: 1777

Transparent JList in JScrollPane

I am currently trying to make a JList opaque that is placed in a JScrollPane. I tried to set everything to setOpaque(false), but it still doesn't work.

Here is the code:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class NavigationView extends JPanel{

    private static final long serialVersionUID = 8033665282200374807L;

    private JScrollPane pane;
    private JList list;

    public NavigationView() {
        this.initialize();
        this.build();
        this.configure();
    }

    public void initialize() {
        this.pane = new JScrollPane();
        this.list = new JList();
    }

    public void build() {
        this.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1;
        gbc.weighty = 1;
        gbc.gridwidth = 1;
        gbc.insets = new Insets(5, 5, 5, 5);
        this.add(this.pane, gbc);
    }


    public void configure() {
        this.pane.setOpaque(false);
        this.pane.setViewportView(this.list);
        this.pane.getViewport().setOpaque(false);
        this.list.setOpaque(false);
        ((DefaultListCellRenderer) this.list.getCellRenderer()).setOpaque(false);
    }
}

If I add this panel to a JFrame with colored background I still see the bounds of the NavigationView-Panel with a white background. What am I doing wrong?

Upvotes: 0

Views: 2054

Answers (2)

StanislavL
StanislavL

Reputation: 57421

I think you also should call this.setOpaque(false);.

But check also L&F. The transparency could be "suppressed" by L&F. I remember similar bug with QuaQua L&F on Mac.

Upvotes: 2

MadProgrammer
MadProgrammer

Reputation: 347334

You don't make the panel (NavigationView) itself transparent

public void initialize() {
    setOpaque(false);
    this.pane = new JScrollPane();
    this.list = new JList();
}

Upvotes: 2

Related Questions