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

11,928

Solution 1

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

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

Solution 2

REEDIT

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

            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) {
            }

Solution 3

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>");
Share:
11,928
Dragon
Author by

Dragon

Updated on July 07, 2022

Comments

  • Dragon
    Dragon almost 2 years

    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.

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

    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.