javaswingawtutilities

AWTUtilities Transparent JFrame


Using this article from sun. I am trying to create a transparent window.

I have one image inside a label on the frame. I want the image to be visible but the frame invisible.

When i use


try {
   Class awtUtilitiesClass = Class.forName("com.sun.awt.AWTUtilities");
   Method mSetWindowOpacity = awtUtilitiesClass.getMethod("setWindowOpacity", Window.class, float.class);
   mSetWindowOpacity.invoke(null, window, Float.valueOf(0.75f));
} catch (NoSuchMethodException ex) {
   ex.printStackTrace();
} catch (SecurityException ex) {
   ex.printStackTrace();
} catch (ClassNotFoundException ex) {
   ex.printStackTrace();
} catch (IllegalAccessException ex) {
   ex.printStackTrace();
} catch (IllegalArgumentException ex) {
   ex.printStackTrace();
} catch (InvocationTargetException ex) {
   ex.printStackTrace();
}

It makes everthing transparent is possible to keep components not transparent.


Solution

  • You could try just setting the alpha channel for the background color of your frame, that shouldn't descend to components.

    window.setBackground(new Color(1.0, 1.0, 1.0, 0.25));
    

    should give you a white, transparent window.