1

I've been successful in making java windows transparent, but I'm having trouble superimposing opaque components on top of those windows. JFrame.setOpacity(0) and AWTUtilities setWindowOpacity all transfer transparency to constituent components. In addition, JFrame.setBackground(0,0,0,0) somehow bleeds transparency to said components.

How can I fix this?

test classes: transparent background, setOpacity, and AWTUtility, respectively

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Color;

public class test {
public static void main(String[] args){
JFrame frame = new JFrame("test");
JLabel label = new JLabel("Label text");
frame.setUndecorated(true);
frame.setBackground(new Color(0,0,0,128));
frame.add(label);
frame.pack();
frame.setVisible(true);
}
}


public class test2 {

public static void main(String[] args){
JFrame frame = new JFrame("test");
JLabel label = new JLabel("Label text");
frame.setUndecorated(true);
frame.setOpacity(.50f);
frame.add(label);
frame.pack();
frame.setVisible(true);
}
}


import com.sun.awt.AWTUtilities;
import java.lang.reflect.Method;
import java.awt.Window;

public class test3 {
public static void main(String[] args){
JFrame frame = new JFrame("test");
JLabel label = new JLabel("Label text");
frame.setUndecorated(true);

try {
Class<?> awtUtilitiesClass = Class.forName("com.sun.awt.AWTUtilities");
Method mSetWindowOpacity = awtUtilitiesClass.getMethod("setWindowOpacity", Window.class, float.class);
mSetWindowOpacity.invoke(null, frame, Float.valueOf(0.50f));
} catch (Exception x){}     

frame.add(label);
frame.pack();
frame.setVisible(true);
}
}

EDIT: I've tried setBackground(0,0,0,0) on Windows, where it works, but it doesn't work properly on Linux (xfce).

septette
  • 11
  • 1
  • 3

1 Answers1

0

Using AWTUtilties.setOpaque(Window, boolean), you can get what you want. Here is an example of a half-transparent label (with a red background):

import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

import com.sun.awt.AWTUtilities;

public class Test3 {

    protected static void initUI() {
        JFrame frame = new JFrame("test");
        JLabel label = new JLabel("Label text");
        label.setOpaque(true);
        label.setBackground(new Color(255, 0, 0, 128));
        frame.setUndecorated(true);

        AWTUtilities.setWindowOpaque(frame, false);
        frame.add(label);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                initUI();
            }
        });
    }
}

Here are some screenshots with different values for the alpha chanel (made on a white background):

Alpha set to 128 (half-transparent):

Half-transparent label

Alpha set to 0 (completely transparent):

Completely transparent label

Alpha set to 255 (completely opaque):

Completely opaque

Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
  • On my computer the label of this example is partially transparent even when I set alpha to 0, even though it shouldn't. I can't seem to get anything working, and I'm starting to think it's because of the platform I'm using. – septette May 17 '12 at 01:35
  • @septette I updated my post with different screenshots with alpha value of the background color set to different values. My config (here) is WinXP with JDK7 (but I am pretty sure it works on JDK6, at least the latest versions) – Guillaume Polet May 17 '12 at 21:47