views:

854

answers:

2

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.

+1  A: 

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.

Bill K
Thanks! This works great for me... on Mac OS X. Does it work on other platforms? In researching transparent window stuff, I've found a number of articles that say other techniques are required on other platforms. http://explodingpixels.wordpress.com/2009/06/19/hud-on-windows-finally/
slothbear
A: 

You need to set the opacity of the child components, something like

childComponent.setOpaque(true);

That will make them opaque, without making the frame opaque.

aperkins