Search code examples
javaswingxhtmljeditorpane

Displaying XHTML in Swing (JEditorPane)


I'm writing a Java application with GUI using Swing. One of the GUI components is a JEditorPane that renders HTML. All the rendering is fine except that it shows in the first line the XML header:

?xml version="1.0" encoding="UTF-8" standalone="no"?>

I've googled a little bit and I've found the same question here and there, but never any answer. I know that using some other projects like "Flying saucer" is an option, but before I start adding other libs to my project - is there a way to make it work with Swing?

Two notes:

  • I can replace JEditorPane with some other component if it can help. I'm not using it for any other features than HTML rendering.
  • I'm currently using a workaround for that, which works fine, but I have a feeling this is an ugly hack:
    html = html.replaceFirst("\\<\\?.*\\?\\>", "");

Solution

  • Flying Saucer is a good choice for XHTML. JEditorPane can't even handle self closing tags and there are no other components that come with the standard JDK that you can use.