title: Using HTML on java string variables
body: <p>I'm right now working on a simple chat program, and I came up with an issue I couldn't find anywhere in the web, maybe because I didn't use the correct words to describe it.</p>
  
  <p>So, I want to simply color part of a text I'm appending to a JTextArea using html, but the problem is the part of the text I want to color is stored in a String variable, and I don't know how to apply the html on it.</p>
  
  <p>this is  what I'm trying to do in code:</p>
  
  <pre><code>String text = new String("Hello");
  String htmlText = new String("&lt;html&gt;&lt;font color='red'&gt;" + text + "&lt;/font&gt;&lt;/html&gt;");
  </code></pre>
  
  <p>But the output is <code>&lt;html&gt;&lt;font color='red'&gt;Hello&lt;/font&gt;&lt;/html&gt;</code></p>
  
  <p>Thanks, and sorry for my bad English.</p>
  
  <p><strong>Edit</strong></p>
  
  <p>I have solved the problem.
  It was because my <code>textPane.setText()</code> method was
  <code>("&lt;html&gt;"+textPane.getText+"&lt;font color='red'&gt;text&lt;/font&gt;&lt;/html&gt;)</code></p>
  
  <p>I shouldn't have used <code>textPane.getText()</code>, I should have used <code>textPane.getDocument().getText(0, text.getDocument().getLength())</code>.</p>
  
  <p>my present code is:</p>
  
  <pre><code>//chatArea is a JTextPane
  //mesArea is just a source where I get some text
  if (!mesArea.getText().trim().equals("")) {
      mesArea.setText(mesArea.getText().trim());
      try {
          if (chatArea.getDocument().getText(0, chatArea.getDocument().getLength()).equals("")) {
              chatArea.setText("&lt;html&gt;&lt;font color='green'&gt;"+user.getNickname()+": &lt;/font&gt;"+mesArea.getText()+"&lt;/html&gt;");
  
          } else {
              System.out.println("what");
              String paneText = new String(chatArea.getDocument().getText(0, chatArea.getDocument().getLength()));
              chatArea.setText("&lt;html&gt;"+paneText+"\n&lt;font color='green'&gt;"+user.getNickname()+": &lt;/font&gt;"+mesArea.getText()+"&lt;/html&gt;");
          }
      } catch (Exception ex) {
          ex.printStackTrace();
      }
      mesArea.setText("");
  } else {
      mesArea.setText("");
  }
  </code></pre>