Search code examples
javaswinglayout-managerseparatorboxlayout

Java/Swing Box Layout with Separator


Here is the code:

Box twoPanelBox= new Box(BoxLayout.Y_AXIS);
twoPanelBox.add(panelA); // red
twoPanelBox.add(new JSeparator(SwingConstants.HORIZONTAL) );
twoPanelBox.add(panelB); // black

And here is what i get:

screenshot of panels

The red and the black panel are displayed as expected, where the seperater ( green box around) has something like a margin between.

How can a avoid this marging, and eliminate this space (grey area)? Thank you


Solution

  • A little unexpectedly, BoxLayout will stretch the separator. However, this dirty hack will help:

    JSeparator separator = new JSeparator(SwingConstants.HORIZONTAL);
    separator.setMaximumSize( new Dimension(Integer.MAX_VALUE, 1) );
    mergeBox.add(separator);