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

3

8

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.

Scorcher answered 27/2, 2013 at 21:18 Comment(0)
C
12

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);
Coquina answered 27/2, 2013 at 22:32 Comment(3)
I really feel I'm inefficient developer (inspite of I'm new to swing and GUI programming). Sometimes I feel that Java Swing lacks a lot of good features and one should implement them himself but my experience doesn't let me do it and it upsets me a lot. Your variant is even greater than Joop Eggen's - it is more flexible, if one can say so.Scorcher
A more full answer, should get the Accept.Horvitz
@JoopEggen, (Offtopic) They are a little bit different, but both are good. I need to check them in the full context - it is what I do now :)Scorcher
H
3

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) {
            }
Horvitz answered 27/2, 2013 at 21:25 Comment(1)
Thanks, I don't believe it can be so easy.Scorcher
D
1

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>");
Devito answered 2/7, 2015 at 0:37 Comment(2)
This does not apply if you are using the pane.getDocument().inserString(...) function to add text to your JTextPaneTosha
This was what i want. ThanksRetail

© 2022 - 2024 — McMap. All rights reserved.