-1

So I'm making a program as a project for school. In this program I have a panel inside a scrollpane. When I click a button a panel with info is added to the panel inside the scrollpane. I can keep adding as much of these panels as I want.

I set the layout of the panel to FlowLayout. I disabled the horizontal scrolling and set the width of the panel to the width of the scrollpane so I neatly get two of those 'forms' next to each other before it starts a new row of panels. Problem is the vertical scrolling doesn't activate so I can see only 1,5 rows of panels. (see picture)

http://i1281.photobucket.com/albums/a518/Bas_Van_den_Steen/Screenshot2014-05-22191813_zps44483b9b.png

I suspect this has something to do with the height of the main panel I had to define if I wanted to set a width. Ideally there should be an option to set the height to 'automatic', but there isn't.

I know scrolling works because when I enable horizontal scrolling and don't set any dimensions for the panel it just keeps adding forms in a single row which I can scroll through.

I think I might need to use another LayoutManager (but I don't have any experience setting those up) or change some of the settings of the scrollpane or main panel. Can someone help me with this?

camickr
  • 321,443
  • 19
  • 166
  • 288
Dragorian
  • 17
  • 3

1 Answers1

1

I set the width of the panel to the width of the scrollpane so I neatly get two of those 'forms' next to each other before it starts a new row of panels. Problem is the vertical scrolling doesn't activate

A FlowLayout is designed to display components horizontally and the preferred size is always based on a single row of components.

Use a different layout manager. Maybe a vertical BoxLayout, or GridBagLayout or GridLayout depending on your exact requirement.

Read the section from the Swing tutorial on Using Layout Managers for more information and working examples.

set the width of the panel to the width of the scrollpane

You should not be manually setting the preferred width of you panel. As I mentioned earlier that is the job of the layout manager. Maybe the GridLayout is closes to what you need.

camickr
  • 321,443
  • 19
  • 166
  • 288