'Issue on label and panel's transparency

Here there is my frame:

https://i.stack.imgur.com/cpIPJ.png

As you can see, there are some components (JLabel, JPanel, JTable, JScrollPane) on which I called the metod setBackgroundColor(new Color(r, g, b, a)). At each update of the frame, these components show new "shadows" of other components of the frame.

How can I eliminate these "shadows"?



Solution 1:[1]

At each update of the frame, these components show new "shadows" of other components of the frame

Swing does not support semi transparent colors correctly since Swing expects components to be either fully opaque or fully transparent.

Therefore you need to make sure the parent component is painted first to reset the background and then manually paint the background of the component:

JPanel panel = new JPanel()
{
    protected void paintComponent(Graphics g)
    {
        g.setColor( getBackground() );
        g.fillRect(0, 0, getWidth(), getHeight());
        super.paintComponent(g);
    }
};
panel.setOpaque(false); // background of parent will be painted first
panel.setBackground( new Color(255, 0, 0, 20) );
frame.add(panel);

See Backgrounds With Transparency for more information and a reusable class that will do this painting for you.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 camickr