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

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
MemLeak
  • 4,456
  • 4
  • 45
  • 84
  • What L&F are you using? Try to put `setContentMargin(new Insets(0,0,0,0))` – nachokk Dec 27 '13 at 14:26
  • 1
    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 JSeparator – mKorbel Dec 27 '13 at 14:32
  • @nachokk: I'm using W7 standard [UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName())] – MemLeak Dec 27 '13 at 14:41

1 Answers1

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);
Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
MemLeak
  • 4,456
  • 4
  • 45
  • 84
  • 1
    not dirty hack, BoxLayout accepting Min/Max and PreferredSize, JSeparator returns own PreferredSize, but there are another two ways – mKorbel Dec 28 '13 at 20:42