Discussion:
JTextPane - What's the maximum length of the string in setText(string)
(too old to reply)
wfs
2006-03-06 01:48:03 UTC
Permalink
Hi All,

Does anyone know the maximum length of a string for
JTextPane.setText(string) ?

I'm attempting to generate an html report for viewing in a JTextPane.

It appears to work fine for small reports (strings around 100,000
characters), but appears to have problems with larger strings (maybe around
1+ meg). (lost my db connection so will have to wait until tomorow to
narrow down the size it starts to 'break' )

TIA

Bill
Thomas Hawtin
2006-03-06 11:42:24 UTC
Permalink
Post by wfs
Does anyone know the maximum length of a string for
JTextPane.setText(string) ?
It should be around 2,000,000,000 characters. More likely it will be
dependent on available memory.
Post by wfs
I'm attempting to generate an html report for viewing in a JTextPane.
HTML will take more memory. Swing text is decidedly suboptimal.
Post by wfs
It appears to work fine for small reports (strings around 100,000
characters), but appears to have problems with larger strings (maybe around
1+ meg). (lost my db connection so will have to wait until tomorow to
narrow down the size it starts to 'break' )
What do you mean by "problems"?

Local databases and unit testing can be very useful.

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
wfs
2006-03-06 11:36:35 UTC
Permalink
Hi Tom,
the program seems to 'hang'....

As it had been a long day, I called it quits at that point. I'll look at
it tonight after work.

Bill

(a COBOL programmer by day, by night a java newbie...)

=========================================

pseudo-code

string report = "";

loop1 for x = 1 to 50

loop2 for y = 1 to 50

report = report + next-report-line (next-report-line will be
about 300 characters on average)

end-loop2

JTexpPane.setText(report);

end-loop1
Post by Thomas Hawtin
Post by wfs
Does anyone know the maximum length of a string for
JTextPane.setText(string) ?
It should be around 2,000,000,000 characters. More likely it will be
dependent on available memory.
Post by wfs
I'm attempting to generate an html report for viewing in a JTextPane.
HTML will take more memory. Swing text is decidedly suboptimal.
Post by wfs
It appears to work fine for small reports (strings around 100,000
characters), but appears to have problems with larger strings (maybe
around 1+ meg). (lost my db connection so will have to wait until
tomorow to narrow down the size it starts to 'break' )
What do you mean by "problems"?
Local databases and unit testing can be very useful.
Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
Morten Alver
2006-03-06 12:02:12 UTC
Permalink
Post by wfs
report = report + next-report-line (next-report-line will be
about 300 characters on average)
If you are actually doing this: "report = report + nextLine" in your
real program,
it may be the cause of your problem. Your should use a StringBuffer and its
append(String) method when concatenating strings this way.

StringBuffer report = new StringBuffer();

In the loop:

report.append(nextLine);

After:

String result = report.toString();



--

Morten
Morten Alver
2006-03-06 12:15:56 UTC
Permalink
Post by Morten Alver
Post by wfs
report = report + next-report-line (next-report-line
will be about 300 characters on average)
If you are actually doing this: "report = report + nextLine" in your
real program,
it may be the cause of your problem. Your should use a StringBuffer and its
append(String) method when concatenating strings this way.
StringBuffer report = new StringBuffer();
report.append(nextLine);
String result = report.toString();
I should mention why this is better: String is immutable, so in your
implementation (provided your pseudocode was accurate) you are for each
iteration creating a new String, and copying everything, which
completely kills performance. StringBuffer is mutable, and intended for
this kind of operations.


--
Morten
wfs
2006-03-06 12:54:50 UTC
Permalink
Thanks Morton,
I altered the logic so it only updated the JTextPane once, rather than
after each cycle of 'loop2'.

It appears that 'JTextPane.setText(report)' is taking several minutes to
execute........ (I'm running the 'app' under JDev..)

Thanks for the suggestions.

Bill

- I'll amend it to use string buffers tonight...

- amend it to produce more diagnostics - length of final report etc etc

=========================================

pseudo-code

string report = "";

loop1 for x = 1 to 50

loop2 for y = 1 to 50

report = report + next-report-line (next-report-line will be
about 300 characters on average)

end-loop2

// JTextPane.setText(report); // commented out the code here -
and moved it to the end.

end-loop1

JTextPane.setText(report); // this took about 2 or 3 minutes.......

==================================
Post by wfs
Hi Tom,
the program seems to 'hang'....
As it had been a long day, I called it quits at that point. I'll look
at it tonight after work.
Bill
(a COBOL programmer by day, by night a java newbie...)
=========================================
pseudo-code
string report = "";
loop1 for x = 1 to 50
loop2 for y = 1 to 50
report = report + next-report-line (next-report-line will be
about 300 characters on average)
end-loop2
JTexpPane.setText(report);
end-loop1
Post by Thomas Hawtin
Post by wfs
Does anyone know the maximum length of a string for
JTextPane.setText(string) ?
It should be around 2,000,000,000 characters. More likely it will be
dependent on available memory.
Post by wfs
I'm attempting to generate an html report for viewing in a JTextPane.
HTML will take more memory. Swing text is decidedly suboptimal.
Post by wfs
It appears to work fine for small reports (strings around 100,000
characters), but appears to have problems with larger strings (maybe
around 1+ meg). (lost my db connection so will have to wait until
tomorow to narrow down the size it starts to 'break' )
What do you mean by "problems"?
Local databases and unit testing can be very useful.
Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
wfs
2006-03-06 23:57:56 UTC
Permalink
Hi All,
using StingBuffer knocked a few seconds off the total time, but the slow
part is;

JTextPane.setText(myStringBuffer) - where myStringBuffer is
about 200k.

This takes about 1 minute to execute.......

Bill
Post by wfs
Thanks Morton,
I altered the logic so it only updated the JTextPane once, rather than
after each cycle of 'loop2'.
It appears that 'JTextPane.setText(report)' is taking several minutes
to execute........ (I'm running the 'app' under JDev..)
Thanks for the suggestions.
Bill
- I'll amend it to use string buffers tonight...
- amend it to produce more diagnostics - length of final report etc etc
=========================================
pseudo-code
string report = "";
loop1 for x = 1 to 50
loop2 for y = 1 to 50
report = report + next-report-line (next-report-line will be
about 300 characters on average)
end-loop2
// JTextPane.setText(report); // commented out the code here -
and moved it to the end.
end-loop1
JTextPane.setText(report); // this took about 2 or 3 minutes.......
==================================
Post by wfs
Hi Tom,
the program seems to 'hang'....
As it had been a long day, I called it quits at that point. I'll look
at it tonight after work.
Bill
(a COBOL programmer by day, by night a java newbie...)
=========================================
pseudo-code
string report = "";
loop1 for x = 1 to 50
loop2 for y = 1 to 50
report = report + next-report-line (next-report-line will
be about 300 characters on average)
end-loop2
JTexpPane.setText(report);
end-loop1
Post by Thomas Hawtin
Post by wfs
Does anyone know the maximum length of a string for
JTextPane.setText(string) ?
It should be around 2,000,000,000 characters. More likely it will be
dependent on available memory.
Post by wfs
I'm attempting to generate an html report for viewing in a JTextPane.
HTML will take more memory. Swing text is decidedly suboptimal.
Post by wfs
It appears to work fine for small reports (strings around 100,000
characters), but appears to have problems with larger strings (maybe
around 1+ meg). (lost my db connection so will have to wait until
tomorow to narrow down the size it starts to 'break' )
What do you mean by "problems"?
Local databases and unit testing can be very useful.
Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
Thomas Hawtin
2006-03-07 10:37:32 UTC
Permalink
Post by wfs
using StingBuffer knocked a few seconds off the total time, but the slow
part is;
JTextPane.setText(myStringBuffer) - where myStringBuffer is
about 200k.
This takes about 1 minute to execute.......
As I say, Swing text is decidedly suboptimal. You might be able to get a
slight performance increase by simplifying the HTML (using simple CSS
styles, for instance). Also make sure the minimum heap size (-Xms) is
set to at least the size your application takes when up and running.

A possibly faster approach would be to apply the styling to the document
yourself. That or use a JTable or similar.

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
wfs
2006-03-07 12:09:07 UTC
Permalink
Hi Tom,

thanks for the help

Bill
Post by Thomas Hawtin
Post by wfs
using StingBuffer knocked a few seconds off the total time, but the
slow part is;
JTextPane.setText(myStringBuffer) - where myStringBuffer
is about 200k.
This takes about 1 minute to execute.......
As I say, Swing text is decidedly suboptimal. You might be able to get a
slight performance increase by simplifying the HTML (using simple CSS
styles, for instance). Also make sure the minimum heap size (-Xms) is set
to at least the size your application takes when up and running.
A possibly faster approach would be to apply the styling to the document
yourself. That or use a JTable or similar.
Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
Loading...