Skip to main content

Continuation tooltips for JLists and JTrees

Posted by timboudreau on August 21, 2006 at 2:30 PM PDT

A bit before NetBeans 5.0 was out, I wrote support for completion tooltips for JLists and JTrees for NetBeans' Explorer components. It's not at all NetBeans-specific - it can be used for any JList or JTree.


What it does is relatively simple - provide tooltip-like popup(s) that are painted by the list or tree's cell renderer. If necessary it will provide two tooltips - one on the left, and another on the right.

The source code can be found here - all you do is call

ViewTooltips.register (theComponent);

where theComponent is a JList or a JTree. The code was written in expedience mode, not prettiness mode, but it has been in NetBeans since 5.0, is stable, and pretty useful, as such tooltips are rather slick and nice to have.

Writing it was a bit of an adventure - the basics were simple until I tried the code out on my Mac. There, heavyweight popups have drop shadows- exactly what you don't want around something that is supposed to look like it's part of another component.


There are two solutions to the problem: The simple one, which works 60-70% of the time on Mac - which is just writing a custom PopupFactory which prefers lightweight popups when possible. But this doesn't entirely work, as the mac implementation will sometimes recycle a heavyweight popup. Both can be found here.

The second solution, that works 100% of the time, is the egregious hack (see the aptly named inner class, EgregiousHack with its giant wad of reflection code), which commits the no-no of requiring Apple's Cocoa-Java, digging up the native window using Cocoa via reflection and forcing it to have no shadow. This is disabled by default in NetBeans, as who knows what the fate of Cocoa Java will be, long-term, but can be turned on.

Related Topics >>