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

Tip: Looking for answers? Try searching our database.

displaying large amount of text in a swing textcomponent

Thread view: 
Robert Ludewig - 14 Apr 2004 01:59 GMT
Hello, I want to display large amount of text in a swing Textcomponent.
I do not want to edit it, it just has to be a plain JTextArea. Of course it
is not a good idea or impossible to load 1 MB text at once into a JTextArea.
I probally have to load and unload chunks dynamically depending where the
users scrolls to.
But how can I implement such a functionality using JTextArea. Can you pleasa
give me some hints.
Especially I have no clue how the communication shall happen between the
scroller (will I be using a JScrollPane ?) and the Text(component) . I
somehow have to fake the behavior of the scrollbars. They need to dipslay
themsilfe relative to the big complete text and not only to the chunks that
are currently loaded. How can that be done. Also someone gave me a hint that
I have to implement my own document model. Can you maybe briefly explain
that procedure , if it applies ?

A little example application that does that would be really helpful. That
would explain everything to me. Maybe you have a link to such an applikation
?

Thank you very much for your help
Roedy Green - 14 Apr 2004 03:55 GMT
>Of course it
>is not a good idea or impossible to load 1 MB text at once into a JTextArea.

Have you tried the experiment?  Before you go off trying to solve the
problem, make sure it really exists.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Robert Ludewig - 14 Apr 2004 16:30 GMT
Yes the problem does exist and yes I have tried it.
When I load really much text, the JTextArea truncatetes some of the text.
Meaning parts of it are not displayed.
I from within a thread I do
SwingUtilities.InvokeLater ( new Runnable () {
jtextarea.append(nextlinestring);
);
Knute Johnson - 14 Apr 2004 22:16 GMT
> Yes the problem does exist and yes I have tried it.
> When I load really much text, the JTextArea truncatetes some of the text.
[quoted text clipped - 3 lines]
> jtextarea.append(nextlinestring);
> );

That could be why it doesn't work.  The Runnable must have a run()
method.  Also, JTextArea.append() is thread safe and does not require
that it be called from the EDT.

Signature

Knute Johnson
email s/nospam/knute/
Molon labe...

Robert Ludewig - 15 Apr 2004 21:20 GMT
Sorry, of cource ! Thi is the way I do it:
But its still not working. The text gets chunked and displayed in worng
order. Or the text get truncated.
Thread t = new Thread(new Runnable()
{
public void run()
{
try
{
brFile = new BufferedReader(new InputStreamReader(new FileInputStream(file),
encoding));
}
catch (UnsupportedEncodingException e2)
{
e2.printStackTrace();
}
catch (FileNotFoundException e3)
{
e3.printStackTrace();
}
char [] chBuff = new char[1024 * 10];
int r = 0;
int i = 0;
try
{
while ((r = brFile.read(chBuff, 0, chBuff.length - 1)) != -1)
{
str = new String (chBuff, 0, r);
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
jtextarea.setCaretPosition(0);
jtextarea.append(str);
}
});
}
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
});
t.start();
Thomas Weidenfeller - 16 Apr 2004 10:27 GMT
> Sorry, of cource ! Thi is the way I do it:
> But its still not working. The text gets chunked and displayed in worng
> order. Or the text get truncated.

(a) Fix the obvious bug in your code:

> while ((r = brFile.read(chBuff, 0, chBuff.length - 1)) != -1)
> {
[quoted text clipped - 7 lines]
> }
> });

If the next iteration of the while loop happens before the event loop
has processed the runnable, then str is replaced with the next buffer,
and the str contents is lost and never added to the text area. Instead
the second str contents will be added twice. If the while loop is much
faster than the event processing, then you get even more of this behavior.

If fixing this bug doesn't fix all your problems, continue with (b).

(a1) Not relevant here, but re-read the API documentation of read() and
a Java textbook, which explains arrays and the java char type:

> while ((r = brFile.read(chBuff, 0, chBuff.length - 1)) != -1)

The "- 1" in "chBuff.length - 1" is unnecessary. Java is not C or C++.

(b) Build a reliable test case: Figure out at which text size the
problems exactly start (up to a one byte precision). Investigate that
number. Does it look familiar? Is it some typical multiple of 2? How
does it look in binary? Does it ring a bell?

(b1) Consider http://www.physci.org/codes/sscce.jsp You might also
consider finally telling us what is a "large file" for you. Are we
talking gigabytes here?

(c) Figure out which of the involved objects has the problem, e.g. by
using a debugger and observing the object's state and looking for wild
changes / overflows when approaching the the magic file size you just
figured out in (b).

(d) Replace that particular object with an own implementation. Potential
candidates are:

- The default Document used by the JTextArea

- The JTextArea widget

- The ScrollPane

- The slider at the scroll pane

/Thomas
Robert Ludewig - 18 Apr 2004 09:45 GMT
hm converting swingutilitiest.invokelater to invokeandwait seem to
completely solve that problem
Just courious,  what did u waht to hint to in (b)?
Roedy Green - 18 Apr 2004 22:27 GMT
>That could be why it doesn't work.  The Runnable must have a run()
>method.  Also, JTextArea.append() is thread safe and does not require
>that it be called from the EDT.

see http://mindprod.com/jgloss/threadsafe.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Roedy Green - 14 Apr 2004 03:56 GMT
>Hello, I want to display large amount of text in a swing Textcomponent.

You could display it as a Swing JTable, one line per slot.  Then there
is no overhead for having a giant table once you have the thing
loaded.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Mark Thornton - 22 Apr 2004 20:33 GMT
> Hello, I want to display large amount of text in a swing Textcomponent.
> I do not want to edit it, it just has to be a plain JTextArea. Of course it
> is not a good idea or impossible to load 1 MB text at once into a JTextArea.
> I probally have to load and unload chunks dynamically depending where the
> users scrolls to.

Actually for a mere 1MB you might well try loading it in one go. If the
text runs to 10's of megabytes then I suggest replacing the Document
implementation with a special purpose one. With a Document
implementation that lazily loads its content it is possible to display
100MB text documents with reasonable response times.

Mark Thornton


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.