tags:

views:

63

answers:

1

I created a JFrame initialized with a BorderLayout and a JScrollPane as its CENTER element.

The scroll pane is set with VERTICAL_SCROLLBAR_ALWAYS and HORIZONTAL_SCROLLBAR_NEVER policies. The intent of my frame is to have a controlled width, while the height should grow/shrink as data is added/removed.

Inside my scroll pane, I added a simple JPanel (lets call it the content panel) which is initialized with a FlowLayout (and LEADING policy).

In order to test this, I simply populate my content panel with 20 JLabel("Item " + n) components where 'n' is the loop counter.

I would expect to see my labels shown on a single row if the frame is large enough and the labels wrap to other lines when I shrink the width. But instead, there is only a single line displayed with no wrapping... ever.

Does anyone know why the flow layout does not wrap when a scroll pane is involved?

If I remove the scroll pane all together and put the content panel directly in the frame, the desired wrapping effect occurs, but if the frame height is shrunk smaller than the content panel height it just disappears.

The idea is that I want my labels to be wrapped when necessary but also always be visible if it means having to scroll up/down.

Any suggestions on how to fix this?

Thanks.

+2  A: 

Wrap Layout gives an explanation and a solution.

camickr
It seems to work exactly as needed/expected so far! Let's hope that Oracle invests more into Swing than Sun was willing to! Thanks a lot camickr!
Jeach
@camickr: Would you say that the FlowLayout has bugs or do you think it was Sun' intent for it to behave this way? The WrapLayout makes so much more sense it should be part of Swing!
Jeach
Its not a bug because there is no code that attempts to dynamically recalculate the preferred size. Its just the way someone designed it to work.
camickr