Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Strange problem with threaded application.  RSS feed

 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for my "strange" subject line. This is so strange, I must be missing something obvious.

below is the run method of a class that extends Thread.



output is a JTextPane. There is a start() command in the constructor for this class. This constructor is call from the init() method of my applet class. The constructor sets Boolean goRun to true. WHen I fire up the applet, the word "check1" appears, so I know the thread has started. What is supposed to happen is that when I click on a button in my GUI, the string variable cmd is set with a string that starts with setboard . The checks are printed to let me know that a particular command has executed. I use this technique for debugging all the time, this is the first time I have tried with threads. There are no there are no other output statements than the ones you see here. Here is the problem. WHen I click the button the output appears as follows...

check3
check2
check1

I cannot for the life of me understand why it would appear in the reverse order that the actual append statements appear in this block of code. Since I am using the append method, and my strings end with newline "\n", and especially since the last time I checked, code blocks are supposed to execute from top to bottom. I'm sure I missing something obvious, I just can't see it.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You say that output is a JTextPane, but JTextPane doesn't have an append() method. Have you extended JTextPane and added a new method? Or is output something else entirely? Please tell us what class it is, really. And if you've written your own append() method, that's the first place I would look for an explanation of this problem - you could have simply gotten something backwards.
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Duly noted, my bad. Here is my output class



the key line here is (should be) doc.insertString( getCaretPosition(), str, mainStyle );

str is added at the caretPosition, no? And the caret position should be the and of the last ting that was appended? Maybe this has nothing to do with threads, I don't know. Thanks.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Hamilton wrote:And the caret position should be the and of the last ting that was appended?


Based on the question mark, that appears to be an assumption on your part. However if writing to the text area didn't change the caret position, then you would see the behaviour you are actually seeing.

Shouldn't take much debugging to do the experiment. (I should mention that I don't know which is correct, I just know which I'm betting on when you do the experiment.)
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, the insertString() method comes from the Document interface, naturally however the API page for this interface does not mention the word caret once.

however, the API doc for the JTextComponent class says...

By default the caret tracks all the document changes performed on the Event Dispatching Thread and updates it's position accordingly if an insertion occurs before or at the caret position or a removal occurs before the caret position.

This would imply that indeed the fact that I am calling this from a new thread that is not the Event Dispatching Thread is the cause of my problem. And since the output is for debugging only, I don't mind reading from bottom to top.
 
John Kimball
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming it's a threading problem...consider writing a small event handler in tandem with addCaretListener and an output buffer (say, a StringBuilder).

There are a couple of synchronization issues to consider, but otherwise it should be straight forward.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Hamilton wrote:This would imply that indeed the fact that I am calling this from a new thread that is not the Event Dispatching Thread is the cause of my problem. And since the output is for debugging only, I don't mind reading from bottom to top.

That sounds plausible. But you really should be doing everything that affects the UI on the EDT. Google for "Swing Threading Tutorial" and read the tutorial for the way to do that while having time-consuming things done in other threads.
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Fred Hamilton wrote:This would imply that indeed the fact that I am calling this from a new thread that is not the Event Dispatching Thread is the cause of my problem. And since the output is for debugging only, I don't mind reading from bottom to top.

That sounds plausible. But you really should be doing everything that affects the UI on the EDT. Google for "Swing Threading Tutorial" and read the tutorial for the way to do that while having time-consuming things done in other threads.


Normally I would agree Paul, but since the information being displayed is only to be displayed and used by myself during development, I feel I can get away with doing it this way, or maybe writing a CaretListener as John said. A key point is that I want to keep my GUI and my engine is separate as possible. When the application is actually "in use" as opposed to "in development", there will be no output of information by this other thread.

I'm kind of spread out on different boards at the moment. If you look at my last post in the follwoing thread, hopefully it will make sense why I am doing it the way i am doing it. if not, I'm all ears.

http://www.coderanch.com/t/446651/Swing-AWT-SWT-JFace/java/Event-Listener-JTextArea
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!