
Spin Open a Detail Pane #39
Chapter 5, Windows, Dialogs, and Frames
|
203
HACK
The Invisible Man
This hack is fairly simple and relies on one fact: you can add a component to
a layout and alternately make it visible and invisible. Its position relative to
other components is preserved when it’s invisible, but it takes up no
onscreen space. So, a spin-open container consists of three components:
• The top component, which is always visible
• The spinner
• The bottom component, whose visibility can be set by clicking on the
spinner
The layout of these three is pretty straightforward, as seen in Example 5-6,
which lists the
MoreInfoPanel class but omits an inner class (for now).
Example 5-6. Laying out the three panel components
public class MoreInfoPanel extends JPanel {
public Component topComponent;
protected SpinWidget spinWidget;
public Component bottomComponent;
public static final int SPIN_WIDGET_HEIGHT = 14;
public MoreInfoPanel (Component tc, Component mic) {
topComponent = tc;
spinWidget = new SpinWidget( );
bottomComponent = mic;
doMyLayout( );
}
protected void doMyLayout( ) {
setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));
add (topComponent);
add (spinWidget);
add (bottomComponent);
resetBottomVisibility( );
}
protected void resetBottomVisibility( ) {
if ((bottomComponent == null) ||
(spinWidget == null))
return;
bottomComponent.setVisible (spinWidget.isOpen( ));
revalidate( );
if ...