Re: JTextArea - how to create it so it just fits the text ?

From: Tarlika Elisabeth Schmitz (invalid_at_invalid.invalid)
Date: 07/08/04


Date: Thu, 08 Jul 2004 12:38:12 +0100

Hallo Jeanette,

Kleopatra wrote:
>
> Tarlika Elisabeth Schmitz wrote:
>
> the 100 is a "magic number" set initially in WrappedPlainView if the
> lineWrap is true.

I searched around in xxxUI but never came across this - do you know the
Swing API by heart ;-) ?

> As already (I'm a bit late here :-) mentioned: that's not supported.

I know, I know; but I find this a really reasonable request ;-)

For one line table headers I managed to set the column size to
Max(fieldDisplayWidth, HeaderWidth).

You see, I don't want to hard-code column widths for each bloody table.
I have business object fields, they are of certain types, if they are
Strings they are of a certain length, and they have localizable
titles,mnemonics,tooltips. The titles are used as field labels but might
also be used as column headers. So, I don't want to stick newlines in
"by hand". I am simply looking for as generic an approach as possible.

Granted, when it comes to designing the form, not all can be done
automagically. But for a table, it should suffice to write
myTable.setNoOfHeaderRows(2);
myTable.setNoOfVisibleColumns (5); (for this one I will start another
thread, Jeanette)

<ot>
Many years ago, long before Java existed, I worked on a GUI project with
100+ forms. The crap front-end didn't allow for much automation. I spend
ages working out optimum layout arrangements (similar to Karsten's
FormLayout), working out column widths etc. Then got someone to "draw"
them. Nightmare. They called me Mrs Pixel because I always spotted it
when a border was out by a pixel.
Now, with all this Java technology, the kind of semi-generated GUI I was
envisaging then, is only a stone's throw away.
</ot>

> A clean way to get it would be to provide a custom ViewFactory, but as
> far as I can see that would involve subclassing BasicTextUI - everything
> in JTextArea is "plain" so there are no hooks to plugin such a factory.

At some point (not during this project) I would like to create my own
GUI components, stuff that I keep on re-using and that behaves in a
certain way. I realize that, to make a proper job of it, I'll have to
provide each component with its UI delegate rather than just fudging
some subclasses ... At the moment I have only time for quick fudges :-(

I haven't through this through completely (it's just the sort of idea
that you wake up with first thing in the morning):
I think what has to be done is:
a) split String into whitespace separated words
b) arrange words in all possible permutations on lines 1-n (n is a
given); not all lines have to be occupied
c) measure width of lines 1-n for each permutation
d) choose the one with the smallest Max(line_1_Width,...,line_n_Width)

I don't think performance is a problem as we're only talking about 4-5
words at maximum and a maximum of 3 lines.
b - how to loop through all permutations will take a bit of thought.

Now, where should this logic go? I have to admit I am fairly new to
Java, so my ideas might be a bit rough around the edges. I presume, if I
was to create a JMultiRowHeader this would have to be placed in
BasicMultiRowHeaderUI. After all, all I want is a getPreferredSize().

>>The ulterior motive for this is that I want to create a JTable with a
>>custom multi-row header renderer. I would like to set the number of
>>header rows but let the column header wrap and assume the minimum width
>>required.
>
>
> Just curious - what should happen on resizing the columns?

Good point. But come to think about it: If the user squashes his columns
and if a column is too small to display the whole cell - tough. You just
get an ellipsis. What should happen to the header? Just emulate the
behaviour of a squashed JTextArea.
I think the most important thing is that the table looks good when fired
up. If the user wants to squeeze the columns, for instance in order not
to have to scroll to the ones outside the viewport*, fine. By then he
knows what each column is.

*See thread JTable "AUTO_RESIZE and horizontal scrolling" which you
answered yesterday, Jeanette.
http://groups.google.co.uk/groups?dq=&hl=en&lr=&ie=UTF-8&threadm=2l2dehF7qr05U1%40uni-berlin.de&prev=/groups%3Fhl%3Den%26lr%3D%26ie%3DUTF-8%26group%3Dcomp.lang.java.gui

-- 
Regards/Gruß,
Tarlika

Quantcast