JTextArea append method doesn't always append  
Author Message
Lee Graba





PostPosted: 2007-7-25 11:22:00 Top

java-programmer, JTextArea append method doesn't always append I have an application with many custom loggers, and a number of JTextArea
instances for displaying the log entries written to the loggers. Each
JTextArea has a thread associated with it that continuously pulls entries
out of a particular logger and appends that text to the JTextArea. Thus,
each entry written to a logger should show up in the associated JTextArea.

The problem I have is that not all the log entries show up in the text area,
and what shows up and what does not can change with each execution. I've
put print statements before and after the log statements, and before and
after the text area append, and all the expected log entries show up at all
those places. Only in the JTextArea is there variability in what shows up.
Is this a bug in JTextArea, or is this some known behavior?

--
=================
Lee Graba
email***@***.com
=================
 
Roedy Green





PostPosted: 2007-7-25 17:02:00 Top

java-programmer >> JTextArea append method doesn't always append >Only in the JTextArea is there variability in what shows up.
>Is this a bug in JTextArea, or is this some known behavior?
When you do that, you generate a repaint which creates an event that
sits in a queue until the Swing thread has nothing better to do that
repaint the JTextArea. Perhaps you are strangling the Swing thread
so it can't keep up.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Lee Graba





PostPosted: 2007-7-26 7:57:00 Top

java-programmer >> JTextArea append method doesn't always append Roedy Green wrote:

>>Only in the JTextArea is there variability in what shows up.
>>Is this a bug in JTextArea, or is this some known behavior?
> When you do that, you generate a repaint which creates an event that
> sits in a queue until the Swing thread has nothing better to do that
> repaint the JTextArea. Perhaps you are strangling the Swing thread
> so it can't keep up.

But it would catch up eventually, wouldn't it? Also, when log messages
don't show up in the JTextArea, it isn't always the last one. Sometimes
one is missing, but a later log message does show up. It seems to be
dropping some of the messages that are appended.

--
=================
Lee Graba
email***@***.com
=================
 
 
Andrew Thompson





PostPosted: 2007-7-26 18:35:00 Top

java-programmer >> JTextArea append method doesn't always append Lee Graba wrote:
>>>Is this a bug in JTextArea, or is this some known behavior?

>> ....Perhaps you are strangling the Swing thread
>> so it can't keep up.
..
>...It seems to be
>dropping some of the messages that are appended.

I will suggest something I thought when I first saw this
thread. "If you go to the effort of preparing an SSCCE*
that demonstrates this behaviour, you might get more
help."

An SSCCE is a specific type of code, that is self
contained, and can demonstrate a problem for others.

This might be difficult to achieve for this problem, since
you maintain it is with an existing, complex system,
but please read the SSCCE document before you
abandon the concept. It might simply require a 'main'
class for the UI with a text area, itself creating several
instances of a single Thread based update class.

Your claim that the GUI class is somehow dropping the
changes, is a hard one to swallow. An SSCCE that
demonstrates the same behaviour might convince others.

* <http://www.physci.org/codes/sscce.html>

BTW - you have searched the *bug* *database* **, right?
All the above is based on the (rash) assumption that your
code does not simply expose an existing bug.

** <http://bugs.sun.com/bugdatabase/index.jsp>

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-gui/200707/1

 
 
Andrew Thompson





PostPosted: 2007-7-26 18:42:00 Top

java-programmer >> JTextArea append method doesn't always append Andrew Thompson wrote:
>>>>Is this a bug in JTextArea, or is this some known behavior?

Oh, but besides all that 'rabbiting on', note that
Swing methods are not 'Thread safe'.

See
<http://java.sun.com/javase/6/docs/api/javax/swing/SwingUtilities.html#invokeLater(java.lang.Runnable
)>

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-gui/200707/1

 
 
Nigel Wade





PostPosted: 2007-7-26 19:16:00 Top

java-programmer >> JTextArea append method doesn't always append Andrew Thompson wrote:

> Andrew Thompson wrote:
>>>>>Is this a bug in JTextArea, or is this some known behavior?
>
> Oh, but besides all that 'rabbiting on', note that
> Swing methods are not 'Thread safe'.
>
> See
>
<http://java.sun.com/javase/6/docs/api/javax/swing/SwingUtilities.html#invokeLater(java.lang.Runnable
> )>
>

However, JTextArea.append() is one of the few Swing methods which is thread
safe:
<http://java.sun.com/javase/6/docs/api/javax/swing/JTextArea.html#append(java.lang.String)>

My first thought was that the multiple threads appending to multiple JTextAreas
was the problem, and that wrapping the calls in invokeLater would be the
solution. Since the method is supposed to be thread safe this ought not to be
the case, if this is indeed the class/method which is being used. It might
still be worth trying anyway...

I agree with your other post. A SSCCE which demonstrates the problem might shed
some light on it.

--
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : email***@***.com
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
 
 
Andrew Thompson





PostPosted: 2007-7-26 20:29:00 Top

java-programmer >> JTextArea append method doesn't always append Nigel Wade wrote:
>>>>>>Is this a bug in JTextArea, or is this some known behavior?
..
>> See
>
><http://java.sun.com/javase/6/docs/api/javax/swing/SwingUtilities.html#invokeLater(java.lang.Runnable
>> )>
>
>However, JTextArea.append() is one of the few Swing methods which is thread
>safe:
><http://java.sun.com/javase/6/docs/api/javax/swing/JTextArea.html#append(java.lang.String)>

My bad.

..
>I agree with your other post. A SSCCE which demonstrates the problem might shed
>some light on it.

(Can't help but) agree with that.

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via http://www.javakb.com

 
 
Roedy Green





PostPosted: 2007-7-26 21:02:00 Top

java-programmer >> JTextArea append method doesn't always append >But it would catch up eventually, wouldn't it
Not necessarily. If you keep it busy in a sleep or doing some
computation, it will never be free to do service the GUI.
If you have anything substantial to do is a GUI computewise that won't
be done in a tenth of as second or so, spin it off on its own thread.
It then must communicate with the GUI components with invokeLater.
see http://mindprod.com/jgloss/swingthreads.html
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
 
Roedy Green





PostPosted: 2007-7-26 21:09:00 Top

java-programmer >> JTextArea append method doesn't always append On Thu, 26 Jul 2007 10:42:11 GMT, "Andrew Thompson" <u32984@uwe>
wrote, quoted or indirectly quoted someone who said :

>Swing methods are not 'Thread safe'.

There are a few exceptions, and JTextArea.append is one of them.

http://mindprod.com/jgloss/swingthreads.html
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
 
Nigel Wade





PostPosted: 2007-7-26 21:54:00 Top

java-programmer >> JTextArea append method doesn't always append Andrew Thompson wrote:

> Nigel Wade wrote:
>>>>>>>Is this a bug in JTextArea, or is this some known behavior?
> ..
>>> See
>>
>><http://java.sun.com/javase/6/docs/api/javax/swing/SwingUtilities.html#invokeLater(java.lang.Runnable
>>> )>
>>
>>However, JTextArea.append() is one of the few Swing methods which is thread
>>safe:
>><http://java.sun.com/javase/6/docs/api/javax/swing/JTextArea.html#append(java.lang.String)>
>
> My bad.

99% (another of the 87% of made up statistics) of the Swing methods are not
thread safe. Unfortunately, to find out you have to read the API doc for each
method, so a it's a good rule of thumb to assume a method isn't thread safe
unless you know it is. Ensuring thread safety in Swing apps. is a very good
habit to get into, and won't hurt [anything except performance].

--
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : email***@***.com
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
 
 
Lee Graba





PostPosted: 2007-7-27 10:19:00 Top

java-programmer >> JTextArea append method doesn't always append Nigel Wade wrote:

> Andrew Thompson wrote:
>
>> Andrew Thompson wrote:
>>>>>>Is this a bug in JTextArea, or is this some known behavior?
>>
>> Oh, but besides all that 'rabbiting on', note that
>> Swing methods are not 'Thread safe'.
>>
>> See
>>
>
<http://java.sun.com/javase/6/docs/api/javax/swing/SwingUtilities.html#invokeLater(java.lang.Runnable
>> )>
>>
>
> However, JTextArea.append() is one of the few Swing methods which is
> thread safe:
>
<http://java.sun.com/javase/6/docs/api/javax/swing/JTextArea.html#append(java.lang.String)>
>
> My first thought was that the multiple threads appending to multiple
> JTextAreas was the problem, and that wrapping the calls in invokeLater
> would be the solution. Since the method is supposed to be thread safe this
> ought not to be the case, if this is indeed the class/method which is
> being used. It might still be worth trying anyway...
>
> I agree with your other post. A SSCCE which demonstrates the problem might
> shed some light on it.
>

Actually, I have done this both ways, calling append in a separate thread,
and within an invokeLater thread. Same results either way.

--
=================
Lee Graba
email***@***.com
=================
 
 
Lee Graba





PostPosted: 2007-7-27 11:30:00 Top

java-programmer >> JTextArea append method doesn't always append Andrew Thompson wrote:

> Lee Graba wrote:
>>>>Is this a bug in JTextArea, or is this some known behavior?
>
>>> ....Perhaps you are strangling the Swing thread
>>> so it can't keep up.
> ..
>>...It seems to be
>>dropping some of the messages that are appended.
>
> I will suggest something I thought when I first saw this
> thread. "If you go to the effort of preparing an SSCCE*
> that demonstrates this behaviour, you might get more
> help."
>
> An SSCCE is a specific type of code, that is self
> contained, and can demonstrate a problem for others.
>
> This might be difficult to achieve for this problem, since
> you maintain it is with an existing, complex system,
> but please read the SSCCE document before you
> abandon the concept. It might simply require a 'main'
> class for the UI with a text area, itself creating several
> instances of a single Thread based update class.
>
> Your claim that the GUI class is somehow dropping the
> changes, is a hard one to swallow. An SSCCE that
> demonstrates the same behaviour might convince others.
>
> * <http://www.physci.org/codes/sscce.html>
>
> BTW - you have searched the *bug* *database* **, right?
> All the above is based on the (rash) assumption that your
> code does not simply expose an existing bug.
>
> ** <http://bugs.sun.com/bugdatabase/index.jsp>
>

Yeah, a short example of the failure is certainly desirable. I'll see if I
can produce one.

--
=================
Lee Graba
email***@***.com
=================
 
 
tar





PostPosted: 2007-8-1 3:00:00 Top

java-programmer >> JTextArea append method doesn't always append Lee Graba <email***@***.com> writes:

> Also, when log messages
> don't show up in the JTextArea, it isn't always the last one. Sometimes
> one is missing, but a later log message does show up. It seems to be
> dropping some of the messages that are appended.

This sounds like it might be a threading issue, despite the fact that
append is supposed to be thread safe.

Do the "dropped" messages ever appear later, out of order?
Do they appear as expected if you also write them, say to System.out?

--
Thomas A. Russ, USC/Information Sciences Institute
 
 
Tom Hawtin





PostPosted: 2007-8-3 4:20:00 Top

java-programmer >> JTextArea append method doesn't always append Nigel Wade wrote:
>
> However, JTextArea.append() is one of the few Swing methods which is thread
> safe:
> <http://java.sun.com/javase/6/docs/api/javax/swing/JTextArea.html#append(java.lang.String)>


Well, JTextArea is /claimed/ to be thread-safe. Now let us look at the code.

public void append(String str) {
Document doc = getDocument();
if (doc != null) {
try {
doc.insertString(doc.getLength(), str, null);
} catch (BadLocationException e) {
}
}
}

That clearly is not thread-safe.

99% of Swing is not claimed to be thread-safe. 99% of that which is
claimed to be thread-safe, isn't. Or thereabouts. You can just about
trust java.awt.EventQueue.invokeLater, but not a lot else.

Tom Hawtin