Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java's FlowLayout does not resize correctly

Tags:

java

layout

swing

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.

like image 468
Jeach Avatar asked Aug 03 '10 05:08

Jeach


Video Answer


1 Answers

Wrap Layout gives an explanation and a solution.

like image 132
camickr Avatar answered Oct 12 '22 21:10

camickr