Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Set Only Prefered Width

Status
Not open for further replies.

edvedafi

Programmer
May 17, 2003
24
0
0
US
I have JPanel that holds several components laid out in a GridBagLayout. That panel is inside of a JScrollPane. What I would like to do is to prevent the JPanel from becoming wider than the screen, and allow it to be as tall as necessary. I would like to force the components inside the panel(JTextAreas & JLabels) to grow vertically to fit their text without growing horizontally.

I tried setting the preferred and maximum width and height on the JScrollPane, but this did not seem to have any effect at all. If I set the maximums on the panel I also get nothing.

The way I am coming closest to what I want is when I set the preferred size on the panel. However if I make the height to small(thinking the layout manager would expand the panel) the panel will expand, but the scroll pane does not think there is anything to scroll up or down to. I tried 0 also, thinking that this would tell the LayoutManager to do whatever it wanted with the height, but I got the same result. It does work if I simply guess at the appropriate height. As long as my preferred height is big enough to fit the entire panel then I have a working scroll bar.

As I am typing this it came to mind that I could calculate the height necessary, however I would rather not do that because I am dynamically creating between 40 and 50 controls on the panel. If this is my only option is there a way to loop through all of the components to easily determine this number?

Any thought are appreciated

Jason
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top