Java/Swing Box Layout with Separator
Asked Answered
A

1

5

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

Ametropia answered 27/12, 2013 at 14:25 Comment(3)
What L&F are you using? Try to put setContentMargin(new Insets(0,0,0,0))Townie
well this is the correct output, (but for other answerers to) for better help sooner post an SSCCE, short, runnable, compilable, just about JFrame with two JPanels and one JSeparatorGuelph
@nachokk: I'm using W7 standard [UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName())]Ametropia
A
11

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);
Ametropia answered 27/12, 2013 at 14:54 Comment(1)
not dirty hack, BoxLayout accepting Min/Max and PreferredSize, JSeparator returns own PreferredSize, but there are another two waysGuelph

© 2022 - 2024 — McMap. All rights reserved.