3

FlowLayout performs "pressure" from the right, so as all components are trying to take their minimal widths. Contrary, BoxLayout tries to spread all components to fill entire height of the space.

Can I add some filler as last component or something to make all components have minimal heights in BoxLayout?

Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385
  • 2
    _their minimal widths_ no: FlowLayout keeps them at prefSize always :) In BoxLayout, add a glue at the end or be sure the children implement max to something reasonable. Best to go for a more powerful layoutManager in the longer run, f.i. MigLayout – kleopatra Sep 29 '12 at 08:13
  • I would use some Grid layout with empty labels to adjust the frame, its not the simplest way, but without any other option, its better than nothing =) – Victor Oliveira Oct 10 '13 at 13:30
  • didnt try it myself, but you might take a look at this https://code.google.com/p/verticalflowlayout/downloads/detail?name=VerticalFlowLayout.java&can=2&q= – Bastiat Nov 12 '13 at 20:18

1 Answers1

1

You could use Box.createGlue(), which returns a component that takes up as much space as the BoxLayout will give it. Adding it to the bottom of a vertical BoxLayout will scrunch the other components to the top.

You could also use nested layouts.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • Now your should be able to comment to your heart's content. :) – Andrew Thompson Jan 01 '14 at 05:50
  • Huzzah! Happy new year to me! :D – Kevin Workman Jan 02 '14 at 13:55
  • LOL! Is there anybody besides (you &) Professor Farnsworth that uses that expression? ;) Oh, and happy new year back at you. :) – Andrew Thompson Jan 02 '14 at 14:07
  • This will sadly not have the desired effect, if the other components of the box are ready to take up more space than they need, e.g. JPanels or JComboBoxes. Then the glue will take up exactly as much space as the other components, not "all that is left". – Zsar Jan 21 '16 at 11:47
  • @Zsar At the risk of being pedantic, the glue does still take up as much space as the BoxLayout will give it- it's just that the BoxLayout gives it zero space. – Kevin Workman Jan 21 '16 at 13:51
  • It still does get as much space as the largest other component. Neither does help to "make BoxLayout behave as vertical FlowLayout", though, which is the question. ... I guess I am the one being pedantic here, because I happen to have the same problem (just as many other people; very similar questions are all over the place), and no one ever answered a good solution. As a matter of fact, [this](http://stackoverflow.com/questions/34292717/how-to-maximise-the-effect-of-box-glues-how-to-prioritise-component-preferred) looks pretty much like a duplicate of the OP here. – Zsar Jan 21 '16 at 14:02