ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Hacking Swing with Undocumented Classes and Properties

by Joshua Marinacci, coauthor of Swing Hacks
08/10/2005

Swing is a powerful toolkit for building desktop applications, but quite often Swing gets a bad reputation for sins of the past. Many times, I've heard, "Swing is slow," "Swing is ugly," and "Swing is too hard." A lot has changed in the almost ten years since Swing was first designed. Chris Adamson and I wrote Swing Hacks to show that you can make fast, attractive, and usable applications quite easily. You just need to know a few tricks.

This article will show you six undocumented features, classes, and properties that let you hack into Swing. I've based the article on hacks that didn't make it into Swing Hacks for one reason or another. Some of them were too short to be full hacks on their own. Some didn't fit nicely into a particular chapter. Some are new things I've learned since publishing the book (technology stands still for no one). Fortunately, the internet provides a way to keep up with the latest developments. As always, please remember that undocumented features are JVM-platform-and-version-dependent. They could disappear in a new version, so use them carefully and always have backup behavior ready.

Hide a Frame from the Windows Task Bar

Related Reading

Swing Hacks
Tips and Tools for Killer GUIs
By Joshua Marinacci, Chris Adamson

Every frame will show up in the Windows task bar with its own icon. If you are using Windows XP, then the application icons may collapse together, but they are still there. However, if you subclass java.awt.Window instead of java.awt.Frame, then your window will not show up in the taskbar or the Alt-Tab hotswitcher. This is because the window won't be registered as a full "application window" that the operating system treats with special care. Instead, the window will be more like a dialog box.

The disadvantage of directly subclassing Window is that you won't get any window decorations. The title bar and maximize, minimize, and resize buttons will be missing. Even the border will be gone. However, depending on your use, this might not be a problem. Toolbar palettes, for example, are fine without borders and should properly be hidden from the taskbar. Also, the recent trend towards mini-application development (Tiger Dashboard, Yahoo Widgets) suggest that the lack of a title bar won't be a problem.

Make Mac OS X Windows Transparent (For Real)

Mac OS X has a graphics API called Quartz that makes it very easy to create translucency effects. This API isn't directly accessible from Java, but Apple's developers did provide a really nice feature that uses Quartz. If you make the background of your frame partially transparent, the desktop and other applications will show through. For example, if you had a frame with a few buttons on it, you could call this right before you set the frame:

frame.setBackground(new Color(0, 0, 0, 0));

This will make the background of the frame completely transparent, allowing other programs to shine through the gaps between the buttons. This line will make the background be red with 50 percent translucency, so the background will be partially visible and tinted through the frame.

frame.setBackground(new Color(1, 0, 0, 0.5f));

The background setting only affects the frame itself, not the components of the frame. If you want those to be transparent, you will need to set their background colors individually. You could also use this effect to create a rounded window by using a PNG image with transparent sections as the background of your main panel. Then the background would shine through the transparent sections, creating the illusion of a shaped window.

Bounce the Mac OS X Dock Icon

Mac OS X introduced a new form of alert: bouncing the dock icon. This is the icon on the bottom of the screen (depending on your setup) that represents your application. You can make this icon bounce when you wish to alert the user. A single bounce represents a simple notification, such as the arrival of new mail. A continuous bounce represents a serious request for the user's attention (a failed disk write would fall into this category). Since this is a Mac-specific feature, there is no standard API for it, and Apple chose not to add it to their official API. However, there is an undocumented class that can do the job.

Apple has wrapped all of their Cocoa APIs with Java classes. These classes have virtually no documentation, but by referring to the Cocoa APIs they came from, you can usually figure them out. The com.apple.cocoa.application package contains an NSApplication object that has lots of useful methods. In particular, you want the requestUserAttention() method, which will make the dock icon bounce.


import com.apple.cocoa.application.*;
// ... 
public void bounceDock() {
	NSApplication app = NSApplication.sharedApplication();
	int id = app.requestUserAttention(
				NSApplication.UserAttentionRequestCritical);
}
// ...

NSApplication has a static method to get the singleton that represents the application. You can then call the requestUserAttention() method with the constant NSApplication.UserAttentionRequestCritical to make the dock bounce continuously. You can use other constants for other behaviors. The return value can be passed to the cancelUserAttentionRequest() method to cancel the alert once the user has responded. Be sure to add the /System/Library/Java/ directory to your classpath when compiling, so that javac will find the Cocoa wrapper classes.

Pages: 1, 2

Next Pagearrow