Search code examples
javajtextpane

Text/Html JTextPane not rendering properly when pasting formatted text


I'm using a JTextPane with content type of text/html, but whenever I copy a formatted text from MS Word and pastes it on the textpane it doesn't get formatted or displays properly. Some of the tags are being displayed like boxes.

I wanted to attach an image but I couldn't cos my reputation isn't up to 10 yet.

Please help me with this.

Thanks.


Solution

  • It's a common mistake. You need to set :

    ((HTMLDocument)myJTextPane.getDocument()).setPreservesUnknownTags(false);
    //considering that you already equipped yout JTextPane with an HTMLDocument.
    //Note that HTMLEditorKit automatically installs one. 
    

    I believe that's enough to fix your issue.