Keilly: Creating a Swing Dashboard Widget - Part 3 (Transparency)

Friday, May 30, 2008

Creating a Swing Dashboard Widget - Part 3 (Transparency)

Now we're talking!

To make the background transparent in Java 6 u10 is simple, just call AWTUtilities.setWindowOpaque(this, false);.

Things for me are more complicated as I'm developing on OSX which doesn't have a u10 build available (Apple are at least a year behind the rest of Java). Luckily OSX has long been able to make Java windows transparent just by setting the window background color a transparent one. Why Sun didn't follow this pattern???

So, to make the JFrame transparent on all platforms:


This gives a distinctly widgetty looking...



OSX is even kinder by allowing this widget to be dragged around, Windows will have to get a special mouse listener later.

7 comments:

Anonymous said...

great post!

just a hint for who ever tries this out:
you have to call makeTransparent() before the setVisable(true);

Randolf Richardson said...

This one is a little bit tricky to get working because the article doesn't mention important details like extending javax.swing.JFrame, but it does work very well.

In fact, it works so well that background colours on JLabels and whatnot are also transparent, even when specified. Heheh.

Oh yeah, the Exception handling code sucks because if one occurs nobody will know which one. You should at least include these two lines in each caught Exception for the benefit of the developer/student/whomever:

System.err.println(e);
System.exit(1);

Gopher said...

Have you got a complete example of this available?

I've gone through the four steps shown (previously I used the SwingLabs libraries to achieve a similar effect), but the edges of the JPanel within my JFrame are always visible still, rather than being rounded off.

So I have a rounded border, but square edges.

Neil Cochrane said...

The complete source code is available linked at the bottom of the final page.

Neil Cochrane said...

The problem with the rounded/squared corners (Windows only I think) is solved later.

Gopher said...
This comment has been removed by the author.
Unknown said...

Hello Keilly,

First of all don't mine my written english.
Am the beginner of the java developer. am doing own management project, so i need dashboard code in java swing. am searching so many times in google not get dashboard code in java swing But your site only contains suitable java dashboard example. I saw one comment from you like "The complete source code is available linked at the bottom of the final page." but not available in bottom of the final page. so, pls send the code to my mail id "kannan896@gmail.com"

Thanks in Advance....