Dodging the Mac's grow box in Java

I stopped using rxvt on Linux a couple of months ago. A friend who was between jobs got sick of hearing me complain that nothing compared to Apple's Terminal, and started to write something that did compare. The result is a new Unix terminal emulator. I won't try to convert you here; read about it and try it yourself.

If you're a Mac user, you probably won't be giving up on Terminal just yet. I've helped out quite a bit, but have little reason to work on making it fit Mac OS better, because it was really Linux that needed a new terminal emulator.

One thing I did do, though, was stop the grow box covering the scroll bar arrows. (Unlike most X11 window managers, which let you resize a window by dragging the frame, Mac OS uses a square area called the "grow box" which lives inside the frame, over your content; it's your application's job to keep out of its way.) Although the solution seems obvious now, it wasn't immediately obvious to me how to do this, or even that it could be done so well.

The situation we had was a JScrollPane filling the entire window. The grow box covered one of the vertical scroll bar arrows. The solution involved making a JPanel that has the same dimensions as the grow box. What are they? Well, it's square, and it fits between the scroll bars, so it must have the same side length as the short side of either scroll bar:

final int size = (int) scrollPane.getVerticalScrollBar().
JPanel growBoxPanel = new JPanel();
Dimension growBoxSize = new Dimension(size, size);

We also pull the JScrollPane's scroll bar out (JScrollPane doesn't mind, as long as the scroll bar continues to exist somewhere). We put it in the CENTER of a new BorderLayout panel, with the grow box-sized spacer in SOUTH:

JPanel sidePanel = new JPanel(new BorderLayout());
sidePanel.add(growBoxPanel, BorderLayout.SOUTH);

Finally, we put that new panel in EAST of the same BorderLayout that the original JScrollPane is CENTER of:

add(sidePanel, BorderLayout.EAST);

Now the only thing that gets covered up is our fake grow box. Perfect!

You can see a screenshot if you follow the link above. Interestingly, if you compare that with an actual Terminal window, you'll see they're very slightly different. I wonder if Apple used magic numbers rather than measuring? Or is the Java scroll bar not exactly the same as the Cocoa one? (This wouldn't be surprising; if you look carefully, it seems that every component is slightly wrong in some way. Swing pop-up menus don't even feel like Cocoa pop-up menus, let alone look like them.)