> 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