HTML in JTextArea of JEditorPane, JTextPane

家住魔仙堡 提交于 2019-12-24 04:44:04

问题


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.append("</b>");
        b.append("[");
        b.append(sender);
        b.append("] : ");
        b.append("</b>");
        b.append(message);
        b.append("</html>");
        b.append("\n");

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

回答1:


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);



回答2:


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;}");



回答3:


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());


来源:https://stackoverflow.com/questions/5915061/html-in-jtextarea-of-jeditorpane-jtextpane

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!