Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / August 2004

Tip: Looking for answers? Try searching our database.

Scrollbar in JScrollPane with JText Area Only Gets Bigger

Thread view: 
Brad J. - 13 Aug 2004 02:06 GMT
Hello,

I'm having a problem with a JTextArea that I have inside a JScrollPane
where the scroll bar gets added/resized fine to accomodate more
information in the underlying JTextArea, but never shrinks back again
or goes away as the information in the JTextArea is removed.  The
document associated with the JTextArea is constantly having text added
and removed from it, so the number of lines it takes in the JTextArea
are constantly changing.

Periodically I execute the following code to make sure that the number
of rows in the JTextArea are sufficient to display all of the text in
the underlying document:

     if(myTextArea.getRows()!=myTextArea.getLineCount())
    myTextArea.setRows(myTextArea.getLineCount()+2);
     pack();

getLineCount supposedly returns the number of lines in the underlying
document, and if that is different than the number of rows in the
JTextArea I update the number of rows.  The '+2' is a hack because I
could never get the most recently added text at the bottom of the
document/JTextArea to display otherwise (maybe related to the other
problem which I'm getting to?).

So, finally the details of the problem:  When the above code causes
the JTextArea to be bigger than it was before (more rows), the
JScrollPane correctly adds or adjusts the vertical scroll bar to allow
scrolling to anywhere in the displayed document.  When the code above
causes the JTextArea to become smaller (less rows), the vertical
scroll bar in the JScrollPane remains exactly the same as it was
before, allowing the user to scrolldown and see lots of empty space
below the bottom of what is actually in the JTextArea.

Does anybody have any clue what I'm doing wrong here?  Is there
something besides  pack() I should be calling on the top level frame
to get the JScrollPane to notice that the JTextArea is smaller and it
should resize its vertical scroll bar?  Or, is there some way to make
the JTextArea automatically have the same number of rows as the
underlying document, avoiding the need to set the rows in the
JTextArea myself?  Right now if I leave out the above code the
JTextArea always stays the same size and the scrollbar never appears.

Any solutions, pointers or suggestions appreciated.  If there is
relevant information I'm not providing, feel free to ask and I'll try
and provide it.

-Brad
Brad J. - 19 Aug 2004 23:57 GMT
So, nobody seemed to have any ideas on this one (or maybe the answer
was obvious), but I figured out a solution on my own and thought I'd
post to save a headache for somebody else with the same problem later.
In my original code, when the JTextArea was constructed both
setPreferredSize() and setMinimumSize() were being called on the
component.  After removing these everything began to work properly.
Apparently using the 'setXXXSize' calls and 'setRows()' on the same
component causes some sort of weird interaction that causes the
JScrollPane to incorrectly handle the scrolling of the JTextArea.  If
anybody understands why or has a pointer to documentation which I
should have seen warning against doing this, feel free to post a
followup.

-Brad

> Hello,
>
[quoted text clipped - 44 lines]
>
> -Brad
Babu Kalakrishnan - 20 Aug 2004 15:51 GMT
> So, nobody seemed to have any ideas on this one (or maybe the answer
> was obvious), but I figured out a solution on my own and thought I'd
[quoted text clipped - 8 lines]
> should have seen warning against doing this, feel free to post a
> followup.

Calling setPreferredSize() is something you should almost totally avoid when
working with Swing components. Once you call setPreferredSize on a component
with hard coded values, it totally disables the automatic computation process
which decides how big a component must be to display its contents (which is
essential for correct scrolling when your content varies).

In fact you'll find that even the lines you had added which set the rowCount
after you change the content are unnecessary now. The JTextArea component will
automatically adjust its preferredSize depending on its content. I would suggest
that you remove those lines as well since it is more likely to hurt the layout
process than aid it.

For JTextAreas use only the setColumns() and setRows() methods to give it a
preferred size. (Or if you want a fixed preferredSize in pixels rather than
rows/columns for your viewport, override the
getPreferredScrollableViewportSize() method to return the appropriate dimension)

BK


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.