debugging gui/event/threading problems
I have a swing applet that regularly does screwy things in the gui.
Text caret focus goes and can't be regained. messed up painting. This
is just simple JTextPanes and StyledDocuments. I have my own threads
that update the gui through calls to the documents, synchronising on
the document and calling invokeLater.
What is a good way to track down problems such as these?
Is there anything wrong with this method, called from a user thread?
private void appendInText(final String owner, final String text) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Logger.log("appendInText(): " + owner + ", " + text);
final AttributeSet attributes;
if (user.equals(owner)) attributes = userAttr;
else if (recipient.equals(owner)) attributes = reciAttr;
else attributes = consAttr;
final StringBuffer builder = new StringBuffer();
builder.append(owner);
builder.append(": ");
synchronized(inDocument) { // explicitly thread safe
final int length = inDocument.getLength(); //
StyledDocument
if (length != 0) builder.insert(0, '\n');
try {
inDocument.insertString(length, builder.toString(),
consAttr);
inDocument.insertString(
length + builder.length(), text, attributes);
}
catch(BadLocationException ex) {
Logger.log(ex);
}
}
}
});
}
TIA ,
Mike W