I wrote a custom Panel which extends JPanel contains another JPanel. The Parent Panel has a BorderLayout w/ the child panel added to center. This ensures the contents will allways fill the area available. Then the Childs layou is box, where the orientation is passed to the top level Panels contructor. Convienence methods are added to the mix to do things like add a nice titled border and separators.
Now you have a component in which you can place other components in an up-down or left right fashion.
For more complex layouts I nest these custom components. Man this Is getting more and more confusing.
package com.krypto.gui;
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
public class KPanel extends JPanel
{
private final JPanel group = new JPanel();
private int orientation = BoxLayout.Y_AXIS;
public KPanel()
{
setup();
} // End Constructor
public KPanel(final int axis)
{
orientation = axis;
setup();
} // End Constructor
private final void setup()
{
setOrientation(orientation);
setLayout( new BorderLayout() );
add( group, BorderLayout.CENTER );
} // End setup method
public final void setOrientation(final int axis)
{
orientation = axis;
group.setLayout(new BoxLayout(group, orientation));
} // End setOrientation method
public final void add( final JComponent comp )
{
if( orientation == BoxLayout.X_AXIS )
{
comp.setAlignmentY( TOP_ALIGNMENT );
}
else
{
comp.setAlignmentX( LEFT_ALIGNMENT );
}
group.add( comp );
} // End add method
public final void addSeparator()
{
addSeparator( 10 );
} // End addSeparator method
public final void addSeparator( final int length )
{
final Dimension newDim = (orientation == BoxLayout.X_AXIS ?
new Dimension(length, 0) :
new Dimension(0, length));
group.add(Box.createRigidArea(newDim));
} // End addSeparator method
public final void setBackgroundColor( final Color color )
{
group.setBackground( color );
} // End setBackgroundColor method
public final void setBorder( final String title )
{
setBorder( createTitledBorder(title) );
} // End setBorder method
public final void setBorder( final int inset )
{
setBorder(BorderFactory.createEmptyBorder( inset,inset,inset,inset ) );
} // End setBorder method
public final void setBorder( final String title, final int inset )
{
setBorder( BorderFactory.createCompoundBorder
( createTitledBorder(title),
BorderFactory.createEmptyBorder( inset,inset,inset,inset ) ) );
} // End setBorder method
public final void setSize( final int width, final int height )
{
final Dimension newDimension = new Dimension( width, height );
setPreferredSize( newDimension );
setMaximumSize( newDimension );
setMinimumSize( newDimension );
} // End setSize method
protected javax.swing.border.Border createTitledBorder(final String title)
{
return (BorderFactory.createTitledBorder
(BorderFactory.createEtchedBorder(), title));
} // End createTitledBorder method
}