javaswinguser-interfaceubuntuawt

How to make transparent JLayeredPane() and JPanel()? It always shows background of super window


How can i completely make this grey panel as transparent, so that i can see only the button "Test" but not the grey box (JPanel or JLayeredPane)

Screen shot: enter image description here

public class win extends JWindow 
{
    ...

    public win() 
    {
        super(new JFrame());
        layers = new JLayeredPane();
        button = new JButton("close");

        this.setLayout (new BorderLayout ());
        ..

        button.setBackground(Color.RED);
        button.setSize(200,200);
        button.setLocation(0,20);
        this.add("North", button);

        JPanel p = new JPanel();
        p.setOpaque(false);
        p.setSize(300, 200);
        p.setLocation(0, 0);
        p.add(new JButton("Test"));

        layers.add(p, new Integer(1));
        layers.setSize(400,300);
        layers.setLocation(400,50);
        layers.setOpaque(false);
        this.add("North", layers);

        canvas.setSize(screenSize.width,screenSize.height);
        this.add("North",canvas);
        //com.sun.awt.AWTUtilities.setWindowOpacity(this, 0.5f); // gives error in my Java version

    }
}

Follow up: installed as recommended, but no luck yet.

ERROR not solved: Exception in thread "main" java.lang.UnsupportedOperationException: The TRANSLUCENT translucency kind is not supported.

Installed:
  compiz-gnome.i686 0:0.9.4-2.fc15                                                      

Dependency Installed:
  compiz-gtk.i686 0:0.9.4-2.fc15           compiz-plugins-main.i686 0:0.9.4-1.fc15     
  libcompizconfig.i686 0:0.9.4-1.fc15      protobuf.i686 0:2.3.0-7.fc15                

Complete!
You have mail in /var/spool/mail/root
[root@example ~]# xdpyinfo | grep -i render
    RENDER
You have mail in /var/spool/mail/root
[root@example ~]# xdpyinfo | grep -i comp
    Composite
    XVideo-MotionCompensation
[root@example ~]# 

Solution

  • See this article or this article. Note that not all environments support all the features (translucency, per pixel transparencyt etc.) described in the article.

    EDIT: On my system (Ubuntu 10.04.2 LTS, Sun java 1.6.0_26) the following code:

      System.out.println("TRANSLUCENT supported:          " + AWTUtilities.isTranslucencySupported(AWTUtilities.Translucency.TRANSLUCENT));
      System.out.println("PERPIXEL_TRANSPARENT supported: " + AWTUtilities.isTranslucencySupported(AWTUtilities.Translucency.PERPIXEL_TRANSPARENT));
      System.out.println("PERPIXEL_TRANSLUCENT supported: " + AWTUtilities.isTranslucencySupported(AWTUtilities.Translucency.PERPIXEL_TRANSLUCENT));
    

    gives:

    TRANSLUCENT supported:          false
    PERPIXEL_TRANSPARENT supported: true
    PERPIXEL_TRANSLUCENT supported: true
    

    EDIT2: Inspired by this discussion, I just installed and configured compiz and now the 'constant opacity level' slider of the web start application at the second article linked above suddenly can be moved to values smaller than 100% and the demo frame actually is translucent. Also the code snipped shown above now prints true for all three kinds of translucency/transparency. And AWTUtilities.setWindowOpacity(..) does not throw any more but produces a transparent window.

    Note for Java 11+ users: The AWTUtilities class has been removed (JDK-8200149). This functionality is available through GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().isWindowTranslucencySupported(WindowTranslucency.TRANSLUCENT).