This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Getting JTextArea to resize within a JScrollPane Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Getting JTextArea to resize within a JScrollPane" Watch "Getting JTextArea to resize within a JScrollPane" New topic
Author

Getting JTextArea to resize within a JScrollPane

Ian Taylor
Greenhorn

Joined: Sep 03, 2012
Posts: 21
Hi there

I am currently writing an app that contains a JTextArea to output information. It is contained within a JScrollPane, the idea being that once there becomes too much data to display on the text area, the text area automatically grows to fit the available data and the scroll pane kicks in to scroll up and down (horizontal is not an issue as I'm using text wrapping).

Unfortunately I can't seem to find the right combination of preferred size and maximum size on either component to get my app to do this. If I set my preferred size to the starting area and the maximum size to something arbitrarily large, then the text area never grows. Once the data gets to the bottom of the visible area it just stops displaying. I can 'fix' the problem by setting the preferred size to something arbitrarily large, then the text area scrolls as required within the scroll pane, but unfortunately the scroll bars are always there and the area is always scrollable regardless of whether there is any data there to fill it. This makes the app technically functional but ultimately ugly. I am using Gridbag layout, I'm not sure if that's relevant.


Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
looks like you've over-complicated everything.

provided you have the right layout manager for the scrollpane,
this is all you need to do
Ian Taylor
Greenhorn

Joined: Sep 03, 2012
Posts: 21
Michael Dunn wrote:looks like you've over-complicated everything.

provided you have the right layout manager for the scrollpane,
this is all you need to do


To be honest, I created the jFrame with Netbeans which added that code for me. Obviously the size variable changes are a result of me messing around trying to get the thing to work.
Ian Taylor
Greenhorn

Joined: Sep 03, 2012
Posts: 21
Fixed.

You're totally right I was overcomplicating it. Removed all the size parameters then it worked just fine.

If a mod wants to move this to the noob forum I totally understand
 
Don't get me started about those stupid light bulbs.
 
subject: Getting JTextArea to resize within a JScrollPane