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.
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.
this is what I'm trying to do in code:
String text = new String("Hello");
String htmlText = new String("<html><font color='red'>" + text + "</font></html>");
But the output is <html><font color='red'>Hello</font></html>
Thanks, and sorry for my bad English.
Edit
I have solved the problem.
It was because my textPane.setText() method was
("<html>"+textPane.getText+"<font color='red'>text</font></html>)
I shouldn't have used textPane.getText(), I should have used textPane.getDocument().getText(0, text.getDocument().getLength()).
my present code is:
//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("<html><font color='green'>"+user.getNickname()+": </font>"+mesArea.getText()+"</html>");
} else {
System.out.println("what");
String paneText = new String(chatArea.getDocument().getText(0, chatArea.getDocument().getLength()));
chatArea.setText("<html>"+paneText+"\n<font color='green'>"+user.getNickname()+": </font>"+mesArea.getText()+"</html>");
}
} catch (Exception ex) {
ex.printStackTrace();
}
mesArea.setText("");
} else {
mesArea.setText("");
}