A
Alan Moore
Yesterday I thought see what I could find on "Closable Tabs in
JTabbedPane". Our own framework library at work did not have anything
useful and neither our COTS libraries. A quick search of the group was
equally unhelpful. At any rate here is a long overdue contribution
that I threw together as a quick investigation of how this could be
done; although, there is likely a more elegant solution out there
still. I have tried to capture the platform look and feel without too
many dependencies on the UI classes. I have found, in general, that
however much fun it can be (NOT) playing with custom UI classes or UI
hacks are often broken with new releases of the JDK.
Unfortunately, they've managed to break it anyway, despite your
precautions. When I run this code under Tiger-beta1, it comes out
thoroughly mangled in all three L&F's. Apparently, it's the new Ocean
theme causing that, because if I comment out the lines that create the
Metal example, the other two paint okay. However, the Motif example
can't seem to find the specified icon (but if I click on the tiny
square that it paints instead, it does close the tab). The Windows
example is a little flaky, in that the close buttons don't always work
the first time I click them. (It also tends to throw NPE's, but
that's due to a bug I've already reported.)
Under 1.4.2, the close buttons don't work if you use the
SCROLL_TAB_LAYOUT option. And, of course, there's no way to make it
paint the icon to the right of the text (where it belongs, IMO). This
is the most elegant attempt I've seen at solving this problem, but
you're trying to do the impossible. There isn't going to be a
satisfactory solution until they rewrite JTabbedPane from scratch. In
fact, they said they were going to do just that for Tiger, but they
seem to have reneged on that promise.