开发者

HTML in JTextArea of JEditorPane, JTextPane

开发者 https://www.devze.com 2023-03-03 04:25 出处:网络
Right I already worked out that JTextArea or JTextField don\'t support HTML. I want to add text to a \"screen\" like a JTextArea and later keep appending text to it.

Right I already worked out that JTextArea or JTextField don't support HTML.

I want to add text to a "screen" like a JTextArea and later keep appending text to it.

I tried with JTextArea which worked wonderfully, but it does not support formatting it seems...

So I tried using JEditorPane's subclass JTextPane, but this one does not have it's append function...

Can someone guid me in the right direction how I easily can append text to a JTextPane or format a JTextArea.

Or if there is any other component better for this please tell me :)

The update method is called by a subject which does this for multiple objects. This just gives a bunch of strings which are formatted and then put in a nice frame to show the user.

@Override
public void update(String channel, String sender, String message) {

    if(channel.equals(this.subject) || sender.equals(subject)){
        StringBuffer b = new StringBuffer();
        b.append("<html>");
        b.append("<b>");
        b.a开发者_StackOverflow中文版ppend("</b>");
        b.append("[");
        b.append(sender);
        b.append("] : ");
        b.append("</b>");
        b.append(message);
        b.append("</html>");
        b.append("\n");

        chatArea.append(b.toString());
    }


Can someone guid me in the right direction how I easily can append text to a JTextPane

Document doc = textPane.getDocument();
doc.insertString(doc.getLength(), "some text", green);

And use attributes instead of HTML, its much easier. For example you could define the "green" attributes to be:

SimpleAttributeSet green = new SimpleAttributeSet();
StyleConstants.setForeground(green, Color.GREEN);


You can use JEditorPane. But when you want to append some Strings that that contain HTML tag you should perform some methods not just a insertString() method.

For example, you have

//setup EditorPane
JEditorPane yourEditorPane = new JEditorPane();
yourEditorPane.setContentType("text/html");

//setup HTMLEditorKit 
HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
yourEditorPane.setEditorKit(htmlEditorKit);

If you only use insertString()

String htmlText = "<div class='test'><b>Testing</b></div>";
Document doc = yourEditorPane.getDocument();
doc.insertString(doc.getLength(), htmlText, null);

The output will be

< div class = 'test' >< b >Testing< /b >< /div >

You should do this method

htmlEditorKit.insertHTML((HTMLDocument) doc, doc.getLength(), htmlText, 0, 0, null);

So the output on your editorpane will be

Testing


Last, if you want to style your editorpane you can use this method

StyleSheet css = htmlEditorKit.getStyleSheet();
css.addRule(".test {color: green;}");


For formatted output the JEditorPAne is probably the best option.

You can use the getText() method of the JEditorPane to get the text that is currently in the pane, then append the new text and call setText() to put everything back.

e.g.

   b.append(chatArea.getText());
   b.append("All the other text")
   chatArea.setTExt(b.toString());
0

精彩评论

暂无评论...
验证码 换一张
取 消