Search code examples
javaswingopacity

Java: Transparent Windows with non-transparent components?


I just met the utilities (com.sun.awt.AWTUtilities) to make your JFrame really transparent. Documentation here. This works very good. Even in Linux with the desktop effects with wobbly windows turned on. But I want to make also a non-transparent component on the transparent JFrame.

Does anyone know, if this is possible, how?

Here is the code I used:

import com.sun.awt.AWTUtilities;

/* "this" is the JFrame */
this.setUndecorated(true);
AWTUtilities.setWindowOpaque(this, true);
AWTUtilities.setWindowOpacity(this, 0.5f);
AWTUtilities.setWindowShape(this, new RoundRectangle2D.Float(0f, 0f, (float) getWidth(), (float) getHeight(), 15f, 15f));

Solution

  • IIUC, Translucent Windows applies to the entire java.awt.Window and contents, but you might try the approach shown below and in this example.

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setBackground(new Color(0f, 0f, 0f, 0.1f));
    f.setUndecorated(true);
    f.add(new JLabel("<html>Testing<br>1, 2, 3</html>"));
    f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);