problem with JTextPane and StateInvariantError

Hi. I am having a problem with JTextPanes and changing only certain text to bold. I am writing a chat program and would like to allow users to make certain text in their entries bold. The best way I can think of to do this is to add <b> and </b> tags to the beginning and end of any text that is to be bold. When the other client receives the message, the program will take out all of the <b> and </b> tags and display any text between them as bold (or italic with <i> and </i>). I've searched the forums a lot and figured out several ways to make the text bold, and several ways to determine which text is bold before sending the text, but none that work together. Currently, I add the bold tags with this code: (note: messageDoc is a StyledDocument and messageText is a JTextPane)

public String getMessageText(){

String text =null;

boolean bold = false, italic =false;

for (int i = 0; i < messageDoc.getLength(); i++){

messageText.setCaretPosition(i);

if (StyleConstants.isBold(messageDoc.getCharacterElement(i).getAttributes()) && !bold){

bold =true;

if (text !=null){

text = text +"<b>";

}

else{

text ="<b>";

}

}

elseif (StyleConstants.isBold(messageDoc.getCharacterElement(i).getAttributes()) && bold){

// Do nothing

}

elseif (!StyleConstants.isBold(messageDoc.getCharacterElement(i).getAttributes()) && bold){

bold =false;

if (text !=null){

text = text +"</b>";

}

else{

text ="</b>";

}

}

try{

if (text !=null){

text = text + messageDoc.getText(i,1);

}

else{

text = messageDoc.getText(i, 1);

}

}

catch (BadLocationException e){

System.out.println("An error occurred while getting the text from the message document");

e.printStackTrace();

}

}

return text;

}// end getMessageText()

When the message is sent to the other client, the program searches through the received message and changes the text between the bold tags to bold. This seems as if it should work, but as soon as I click on the bold button, I get a StateInvariantError. The code for my button is:

publicvoid actionPerformed(ActionEvent evt){

if (evt.getSource() == bold){

MutableAttributeSet bold =new SimpleAttributeSet();

StyleConstants.setBold(bold,true);

messageText.getStyledDocument().setCharacterAttributes(messageText.getSelectionStart(), messageText.getSelectionStart() - messageText.getSelectionEnd() - 1, bold,false);

}

}//end actionPerformed()

Can anyone help me to figure out why this error is being thrown? I have searched for a while to figure out this way of doing what I'm trying to do and I've found out that a StateInvariantError has been reported as a bug in several different circumstances but not in relation to this. Or, if there is a better way to add and check the style of the text that would be great as well. Any help is much appreciated, thanks in advance.

[5177 byte] By [RockGuitar417a] at [2007-11-27 11:00:21]
# 1

Swing related questions should be posted in the Swing forum.

Can't tell from you code what the problem is because I don't know the context of how each method is invoked. But it would seem like you are trying to query the data in the Document while the Document is being updated. Try wrapping the getMessageText() method is a SwingUtilities.invokeLater().

There is no need to write custom code for a Bold Action you can just use:

JButton bold = new JButton( new StyledEditorKit.BoldAction() );

Also your code to build the text String is not very efficient. You should not be using string concatenation to append text to the string. You should be using a StringBuffer or StringBuilder.

camickra at 2007-7-29 12:29:07 > top of Java-index,Java Essentials,Java Programming...
# 2

Thank you camickr. The reason I tried to write my own custom code for bold was that originally, using the StyledEditorKit didn't work when adding the <b> tags. However, that must have been when I was doing something different in that part of the code and it works not when I do what you said. Thank you very much.

RockGuitar417a at 2007-7-29 12:29:07 > top of Java-index,Java Essentials,Java Programming...