Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 620
Dragon Avatar asked Feb 27 '13 21:02

Dragon


2 Answers

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);
like image 74
camickr Avatar answered Sep 18 '22 12:09

camickr


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) {
            }
like image 40
Joop Eggen Avatar answered Sep 20 '22 12:09

Joop Eggen