1

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("");
}
6
  • 4
    Possible duplicate of How to use html tags in JTextArea Commented Oct 11, 2015 at 17:27
  • If this is the observed output [ <html><font color='red'>Hello</font></html> ] can you please describe how your expected output would look like?. Commented Oct 11, 2015 at 17:50
  • @Tom I tried what they said, but now it just prints out nothing! Commented Oct 11, 2015 at 18:54
  • @Pavan What I expect is the word hello in red, and if there was more text (after the </font>) ),then I still want only the word hello in red. Commented Oct 11, 2015 at 18:57
  • @IdoFangBentov If you tried the answers from the linked question and they didn't work for you, then please update your question and post your current code and your current results. Please read this help page to know how to format code correctly. Commented Oct 11, 2015 at 20:04

1 Answer 1

5

You can use JTextPane / JEditorPane instead of JTextArea like

String text = new String("Hello");
String htmlText = new String("<html><font color='red'>" + text + "</font></html>");
JTextPane jTextPane =new JTextPane ();
jTextPane.setContentType("text/html");
jTextPane.setText(htmlText);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.