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 / September 2004

Tip: Looking for answers? Try searching our database.

Expanding JTextArea depending on text inserted

Thread view: 
Duc - 03 Sep 2004 01:52 GMT
I want a JTextArea to always be 250 pixels wide (to keep a FlowLayout
the proper length) but also be able to expand in the y direction as
large as possible, depending on the size of the text inserted. Small
text, short box. Lots of text, long box. I don't want a scrollbar or
anything like that.

It's driving me crazy. setMaximumSize(new Dimension(250, 10000)) almost
does this, but the textarea doesn't stretch all 250 pixels. For some
crazy reason it decides to stop at around 100 pixels, but it's
otherwise what I want (I have word wrapping on; stretches in the y
direction properly). How do I get it to use its maximum width?
Thank you in advance.
Christian Kaufhold - 03 Sep 2004 14:11 GMT
> I want a JTextArea to always be 250 pixels wide (to keep a FlowLayout
> the proper length) but also be able to expand in the y direction as
[quoted text clipped - 8 lines]
> direction properly). How do I get it to use its maximum width?
> Thank you in advance.

Please post your compilable code.

Christian
Babu Kalakrishnan - 03 Sep 2004 14:16 GMT
> I want a JTextArea to always be 250 pixels wide (to keep a FlowLayout
> the proper length) but also be able to expand in the y direction as
[quoted text clipped - 8 lines]
> direction properly). How do I get it to use its maximum width?
> Thank you in advance.

Try calling setSize(250,25) on it before inserting it into the container with
FlowLayout. The value of the second parameter - (i.e the height) what you
suggest isn't important - but the TextArea will generally try not to change its
width once it has already been set.

An alternate way is to call setColumns(n) on it where "n" is a value that gives
you the desired width. This is probaby a little more stable because this value
is always taken into account to compute the preferred size - but then this
doesn't allow it to set the width in pixels.

BK
Duc - 03 Sep 2004 14:41 GMT
> Try calling setSize(250,25) on it before inserting it into the container with
> FlowLayout. The value of the second parameter - (i.e the height) what you
> suggest isn't important - but the TextArea will generally try not to change its
> width once it has already been set.

If a shrine is ever erected in your honor, rest assured I will worship
at it. Or I'll buy you a beer.

Your suggestion worked beautifully and I my understanding of the
difference between setSize() and setPreferredSize() are a little better
now (I've always used setMinimumSize(), setMaximumSize(), and
setPreferredSize() since those are the 3 options JBuilder shows).

Thank you kindly! The successful code is below (I had to type it
manually since the actual code is on a classified network, so excuse
typos):

in a loop
{
...

epTextArea[i].setText(epSet.toString());
epTextArea[i].setBackgroundColor(Color.lightGray);
epTextArea[i].setSize(new Dimension(250, 17));
epTextArea[i].setLineWrap(true);
epTextArea[i].setWrapStyleWord(true);
epTextArea[i].setEditable(false);
...

  epSetPanel.add(epTextArea[i], null);

  ...
}
Babu Kalakrishnan - 03 Sep 2004 15:40 GMT
> Your suggestion worked beautifully and I my understanding of the
> difference between setSize() and setPreferredSize() are a little better
> now (I've always used setMinimumSize(), setMaximumSize(), and
> setPreferredSize() since those are the 3 options JBuilder shows).

Don't really go overboard with that improved "understanding" - because the
setSize() solution is more of a hack than a real solution - depending only on
the current implementation of Sun's library code. It only works because in the
case of a TextArea, the preferredSize is not really unique, and can be one of
several values depending on the preferred width, and the implementation of the
getPreferredSize method first checks if size was ever set on it earlier, and if
so returns a preferredSize parameter that matches that width (it is also more
efficient because it doesn't have to tear down all the views and recompute line
breaks from scratch).

In general, external setSize() calls on components don't do anything meaningful
because the value set by them get wiped out once the LayoutManager performs a
layout. The JTextArea class (or any other JTextComponent capable of wrapping)
should have really added a setPreferredWidth() method which allows a user to
specifiy a preferred width independent of the height. But unfortunately it
doesn't exist and the closest you can get is the setColumns() method. (That is
the reason why I said in my message that the setColumns(n) call may be more
"stable")

Also in my view, calling setPreferredSize() on any component is almost always
the wrong thing to do because it totally defeats the automatic layout management
mechanism employed by swing.

BK
Fred L. Kleinschmidt - 03 Sep 2004 15:18 GMT
> I want a JTextArea to always be 250 pixels wide (to keep a FlowLayout
> the proper length) but also be able to expand in the y direction as
[quoted text clipped - 8 lines]
> direction properly). How do I get it to use its maximum width?
> Thank you in advance.

If you want the JTextArea to be 250 pixels wide, either use setSize or
setMinimumSize. Calling setMaximumSize(250,10000) merely tells it that
it cannot be expanded to a width greater than 250. Calling
setMinimumSize(250,20) tells it that it must be at least 250 wide.

Signature

Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94  (206)544-5225



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



©2012 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.