How can I restrict the size of a component in a BoxLayout?

Scott Stanchfield

BoxLayout respects the preferred, maximum and minimum sizes of its managed components.

You can manage the sizes in two ways:

  • Subclass your components and override their sizing methods:
    • getPreferredSize()
    • getMaximumSize()
    • getMinimumSize()
  • Drop the components inside a decorator that will manage the sizes.

I've created a set of beans that can help work with BoxLayout. See http://javadude.com/tools for "BoxBeans". In particular, the BoxAdapter bean decorates any component and provides four properties to control the maximum and minimum sizes of the contained components.