Translucent Windows with Swing

y chance I stumbled across a solution to problem long unnerving me: translucent windows with Swing. We are developing a Java application for till systems with a touchscreen GUI. Part of the applications styleguides includes dialogs and popups with rounded edges, somewhat like the windows in Mac OS X. While we managed to get the rounded edges and a tiled background right with images arranged precisely through a custom border, they were never really rounded in a technical sense. There were always small rectangular remains at the edges, because the JPanel itself was rectangular of course. All we could do was set the color of the dialogs background pane to something very closely matching the overall background color scheme to have these remains as unobtrusive as possible. Every person I asked told me about using JNI and some native code, as long as the underlying platform supported alpha channels and things like that. As the application must run correctly on both Windows (2000/XP) and X11/Linux, this was not a viable option and so we were prepared to resign to the fact, that there was no way to do it... Until today!

By pure coincidence when looking for something completely different (not even Java related) I found this page. It is an excerpt from O'Reilly's Swing Hacks book. It contains a piece of code that gives you the effect of a seemingly translucent window by taking a snapshot of the screen contents behind it and use part of it as the background image for the dialog to be opened. This effectively makes the background shine through the parts of the dialog you do not explictly use for something else. The only thing that does not work is the automatic update in case the dialog is moved around on the screen (resizing is handled, however). But this is no problem for us, as we only use undecorated popups that cannot be moved anyways.

I wonder if I should buy the book and see what other cool stuff can be done without resorting to JNI :-)

Update: I have written a follow-up more recently. Be sure to have a look at it, too.


Popular posts from this blog

SAXParseException: -1:-1: Premature End Of File - Misleading error

Amiga 500 Restoration: A501 Memory Expansion

Amiga 500 Restoration: Mouse and Mainboard Maintenance