Need some advice...
I have a program that needs to edit a JTextPane in two different ways, but by its nature, one set of the calls will be from a separate Thread'ed source while the other is associated with the Swing/AWT Thread (I'm not sure which, and I'm not even sure if I have this concept. Basically, the second one edits the text in the pane when the enter key is pressed.). The result is that when these two sources trigger at almost the same time, as they inevitably do, the text displayed in the box becomes mangled, or the program crashes. Do you all have an ideas for how to solve this? Would SwingUtilities.invokeLater work here?
My only idea was to have a boolean that detected if an edit event was being parsed, and if something tried to edit it while this was true, it would store the change data in an ArrayList or something. Then at the end of each parsing, run through the list if it was non-empty and apply those changes. The problem with this is that there is still a chance of overlay between change events, albeit smaller.
Any other ideas?
Thanks,
Andy
[1088 byte] By [
Zyphona] at [2007-10-3 8:29:55]

If code is being executed in a Listener then its executing on the EDT and you don't have to worry.
If code is executing in a separate Thread then yes you would typically use SwingUtilities.invokeLater(..). However, if you read the API for the AbstractDocument, you will find that the insertString(...) method is Thread safe so you shouldn't need to worry.
Although you haven't really described your problem so we can't give any further advice.
If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.
[nobr]Since I can't easily adapt my code into a Short, Self Contained, Compilable and Executable, Example Program (SSCCE), here is some code I'll try to explain:
public void receiveMessage(String msg, String user)
{
String tmp = messagesEditorPane.getText();
messagesEditorPane.setText(tmp.substring(0, tmp.length() - 16) + "<font color = #0000FF>" + user + ": </font>" + msg + "<br></body></html>");
}
public void postMessage(String msg)
{
String s = "";
if(!((s = chatTextArea.getText()).equals("")))
{
String tmp = messagesEditorPane.getText();
messagesEditorPane.setText(tmp.substring(0, tmp.length() - 16) + "<font color = #FF0000>" + myUsername + ": </font>" + msg + "<br></body></html>");
}
}
messagesEditorPane is a JEditorPane (I accidently said JTextPane originally)
postMessage is called when the "Enter" key is pressed, pulling the text from one textArea and adding it to the messagesEditorPane text.
receiveMessage is called when a Thread listening to an InputStream from a Socket recieves data, passing it the data and default username (unimportant). Basically, when I postMessage, I also send a message to a server, which right now just sends it back, triggering the Socket read Thread, calling receiveMessage. The problem is, if I type text and hit enter really fast in succession, the server replies will start coming in while I am posting more data. These two calls start editing the messagesEditorPane at the same time, screwing up the the text inside it.
Thanks for the reply, and I hope to hear from you again,
Andy[/nobr]
> Since I can't easily adapt my code....
Of course you can if you really understand your problem. You have a Swing Timer that simulates the user typing data and hitting enter to update the text component.
Then you have a separate non- GUI thread the simulates the response from the server that updates the text component.
The snippets of code you posted don't really help. It looks like you are replacing then entire text in the text component every time you get a message. Not a very good idea.
Also, I've never had much luck appending HMTL to the end of an editor pane. I would use a JTextPane instead and then just set the attributes of the text as you add it. This posting give a simple example of this approach:
http://forum.java.sun.com/thread.jspa?forumID=57&threadID=342068
Is there a way to not have to replace the entire text each time? To my knowledge there isn't a addText method or anything... I'll search around and see if I can find how to do it.
All I could really find to not do setText(getText() + s) was the JTextArea's append method (I definitely don't want to use a JTextArea though). The JEditorPane's setText method says it's Thread safe, so maybe this isn't the problem, but it's the only thing that makes sense to me, since it only breaks if I add text really fast in succession.
> Is there a way to not have to replace the entire text each time? I gave you my advice above, including a working example.
Thanks for the help, I misinterpreted that part of your demo. I'll see if I can get this working and post back.
Using a styled document (much better than using "text/html") not only solved the stated problem, but also a weird bug where the scrollbar was spazzing out whenever I added text (probably because I was removing then adding a bunch of text every time). Thanks for your time~Andy