Search code examples
javaswingvertical-alignmentboxlayout

Vertical Aligning BoxLayout Spacing?


When using a BoxLayout that aligns vertically, how do you stop components from expanding as the containing JFrame grows, leaving the extra space at the bottom? Ive tried different layouts, and it just got messy really quickly. Ive also tried adding glue to the bottom of the panel, however the layout manager is treating the glue like another empty JPanel, still causing all of the components to grow. Any suggestions?


Solution

  • Consider nesting your containers. For instance if you place the BoxLayout using container in the BorderLayout.NORTH position of a container that uses BorderLayout, then it will take up only the vertical space it needs and no more.

    Edit: fixed as per lnballoti!