JDK-6461996 : Problem with setOpaque(true) and transparency
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2006-08-21
  • Updated: 2011-02-16
  • Resolved: 2006-08-21
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.5.0_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows 2000 [Version 5.00.2195]

A DESCRIPTION OF THE PROBLEM :
[Refer to test source code]

I have an application which uses a partially transparent glass pane.  I achieve this by using setOpaque(true) and setting the background color with an alpha value of 100.

Run the attached source code and click on the "Start Busy" button.  You will see that the "Start busy" button is drawn in the wrong place underneath the glass pane.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the included source code.  Click the "Start Busy" button.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
A partially transparent glass pane should be displayed with all "underlying" components drawn in the correct place.
ACTUAL -
The components under the glass pane are not all drawn in the correct place.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;

public class Test {

    public static void main(String args[]) {

        final JFrame test = new JFrame("Transparency Test");
        JPanel panel = new JPanel(new BorderLayout());

        panel.add(new JLabel("NORTH", JLabel.CENTER), BorderLayout.NORTH);
        panel.add(new JLabel("SOUTH", JLabel.CENTER), BorderLayout.SOUTH);
        panel.add(new JLabel("EAST", JLabel.CENTER), BorderLayout.EAST);
        panel.add(new JLabel("WEST", JLabel.CENTER), BorderLayout.WEST);

        JPanel glassPanel = new JPanel();
        glassPanel.setOpaque(true);
        glassPanel.setBackground(new Color(200, 200, 200, 100));
        glassPanel.setLayout(new GridBagLayout());
        glassPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));

        JButton busyButton = new JButton("Start Busy");

        busyButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                Runnable busy = new Runnable() {
                    public void run() {

                        Component gp = test.getGlassPane();

                        gp.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                        gp.setVisible(true);
                        gp.requestFocus();

                        try {
                            Thread.sleep(5000);
                        } catch (Throwable t) {
                            // Ignore...
                        }

                        gp.setVisible(false);
                        gp.setCursor(null);
                    }
                };

                Thread t = new Thread(busy);
                t.start();
            }
        });

        panel.add(busyButton, BorderLayout.CENTER);

        test.setGlassPane(glassPanel);

        test.getContentPane().add(panel);

        test.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        test.pack();
        test.show();
    }
}
---------- END SOURCE ----------

Comments
EVALUATION This is not a bug. Swing does not support setting a translucent background color in combination with setOpaque(true). If you want to set a background color with alpha, you need to setOpaque(false), and take care of filling the component's background yourself. See 5001982 for more information.
21-08-2006