java JTextPane 的 setContentType("text/html") 无法正常工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15122611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:33:06  来源:igfitidea点击:

setContentType("text/html") for JTextPane doesn't work as it is expected

javahtmlswingjtextpane

提问by Dragon

When you setContentType("text/html") it is applied only for the text that is set via JTextPane.setText(). All other text, that is put to the JTextPane via styles is "immune" to content type.

当您 setContentType("text/html") 时,它仅适用于通过 JTextPane.setText() 设置的文本。通过样式放置到 JTextPane 的所有其他文本对内容类型“免疫”。

Here is what I mean:

这就是我的意思:

private final String[] messages = {"first msg", "second msg <img src=\"file:src/test/2.png\"/> yeah", "<img src=\"file:src/test/2.png\"/> third msg"};

public TestGUI() throws BadLocationException {
    JTextPane textPane = new JTextPane();
    textPane.setEditable(false);
    textPane.setContentType("text/html");

    //Read all the messages
    StringBuilder text = new StringBuilder();
    for (String msg : messages) {
        textext.append(msg).append("<br/>");
    }
    textPane.setText(text.toString());

    //Add new message
    StyledDocument styleDoc = textPane.getStyledDocument();
    styleDoc.insertString(styleDoc.getLength(), messages[1], null);

    JScrollPane scrollPane = new JScrollPane(textPane, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

    //add scrollPane to the main window and launch
    //...
}

In general, I have a chat that is represented by JTextPane. I receive messages from server, process them - set text color for specific cases, change smile markers to images path etc. everything is made within the bounds of HTML. But as it can be clearly seen from example above, only the setText is the subject of setContentType("text/html") and the second part, where new message added is represented by "text/plain" (if I'm not mistaken).

一般来说,我有一个由 JTextPane 表示的聊天。我从服务器接收消息,处理它们 - 为特定情况设置文本颜色,将微笑标记更改为图像路径等。一切都在 HTML 的范围内进行。但是从上面的例子中可以清楚地看出,只有 setText 是 setContentType("text/html") 的主题,第二部分,其中添加的新消息由“text/plain”表示(如果我没记错的话)。

Is it possible to apply "text/html" content type to all data that is inserted to JTextPane? Without it, it is almost impossible to process messages without implemention of very complex algorithm.

是否可以将“text/html”内容类型应用于插入到 JTextPane 的所有数据?没有它,几乎不可能在不实现非常复杂的算法的情况下处理消息。

回答by camickr

I don't think you should be using the insertString() method to add text. I think you should be using something like:

我认为您不应该使用 insertString() 方法来添加文本。我认为你应该使用类似的东西:

JTextPane textPane = new JTextPane();
textPane.setContentType( "text/html" );
textPane.setEditable(false);
HTMLDocument doc = (HTMLDocument)textPane.getDocument();
HTMLEditorKit editorKit = (HTMLEditorKit)textPane.getEditorKit();
String text = "<a href=\"abc\">hyperlink</a>";
editorKit.insertHTML(doc, doc.getLength(), text, 0, 0, null);

回答by Joop Eggen

REEDIT

编辑

Sorry, I misunderstood the problem: inserting a string as HTML. For that one needs to resort to the HTMLEditorKit capabilities:

抱歉,我误解了这个问题:将字符串作为 HTML 插入。为此,需要求助于 HTMLEditorKit 功能:

            StyledDocument styleDoc = textPane.getStyledDocument();
            HTMLDocument doc = (HTMLDocument)styleDoc;
            Element last = doc.getParagraphElement(doc.getLength());
            try {
                doc.insertBeforeEnd(last, messages[1] + "<br>");
            } catch (BadLocationException ex) {
            } catch (IOException ex) {
            }

回答by Malith

Here is a much simpler way to do that.

这是一个更简单的方法来做到这一点。

JTextPane pane = new JTextPane();
pane.setContentType("text/html");

pane.setText("<html><h1>My First Heading</h1><p>My first paragraph.</p></body></html>");