title: Change styles of existing HTMLDocument content in JTextPane
body: <p>I am trying to build a chat client that uses JTextPane to display the conversations. I am having problems with highlighting the past sentences of a chat-participant chosen by the user. When implementing this I need to stick to using HTMLDocument as some of the content has to be HTML.</p>
  
  <p>The best idea seemed to be using a different named styles for each user participating in the conversation. The idea is that when the text of a particular person needs to be highlighted, I just update his personal style and everything he has said should get highlighted as by magic. Unfortunately this does not work.</p>
  
  <p>So to add text I use:</p>
  
  <pre><code>public void addMessage(String from, String message){
      HTMLDocument doc = (HTMLDocument) textPane.getStyledDocument();
      if(doc != null){
          try {
              String stylename = "from_" + from;
              if(textPane.getStyle(stylename) == null){
                  LOG.debug("Did not find style. Adding new: " + stylename);
                  Style nameStyle = textPane.addStyle(stylename, null);
                  StyleConstants.setForeground(nameStyle, Color.black);
                  StyleConstants.setBold(nameStyle, true);
              }else{
                  LOG.debug("Found existing style: " + textPane.getStyle(stylename));
              }
              doc.insertString(doc.getLength(), from + ": ", textPane.getStyle(stylename));
              doc.insertString(doc.getLength(), message + "\n", null);
          } catch (BadLocationException ble) {
              LOG.error("Could not insert text to tab");
          }
      }
  }
  </code></pre>
  
  <p>So far so good... The text gets displayed in the textPane as I wished. However when I try to update the stylesheet at some future point and call:</p>
  
  <pre><code>public void highlight(String name, boolean highlight){
      Style fromStyle = textPane.getStyle("from_" + name);
      if(fromStyle != null){
          LOG.debug("found style, changing color");
          StyleConstants.setForeground(fromStyle, Color.red);
      }else{
          LOG.debug("fromStyle was NULL");
      }
  }
  </code></pre>
  
  <p>..I can see that the style is found and my code gets executed - yet nothing changes on the screen. </p>
  
  <p>I would like to ask if you have any suggestions on how I might try to resolve this issue. Is there a way to make this work with named styles or should I take some completely different approach?</p>
  
  <p>Thank you</p>