4

I am working on a Swing application in which I have to show HTML files.

I am using JEditorPane control for it. It is showing the content of HTML file but not in the same format as originally in the actual HTML file.

JEditorPane does not support the object element. Rather than loading the object file its just showing ?.

Is there any other inbuilt control in Swing to browse the HTML file rather than JEditorPane?

I am using the following code:

JEditorPane HtmlPane= new JEditorPane();
File file1= new File("path of the file");
HtmlPane.setContentType("text/html");
HtmlPane.setEditable(false);
HtmlPane.setPage(file1.toURI().toURL());
JScrollPane jsp=new JScrollPane(HtmlPane);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
adesh
  • 1,157
  • 3
  • 18
  • 28

3 Answers3

4

Your best bet for more advanced HTML rendering in core Java is to use the JavaFX based WebView component as described in Adding HTML Content to JavaFX Applications. It supports HTML5, but I've not actually played with it, so I cannot say how well it renders HTML.

Note that Java FX is effectively an alternative to Swing, but AFAIU Java FX components can be embedded in Swing GUIs.

jewelsea
  • 150,031
  • 14
  • 366
  • 406
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
2

Support for HTML in Swing Components is limited to 3.2, predating the <object> tag. You might be able to use a HyperlinkListener, illustrated here, in conjunction with Desktop#browse(). See also this example.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 2
    +1, but with the amount of trouble I had shoehorning an applet element into a `JEditorPane` together with what it typically does to subtly styled HTML, I feel there is really nothing that can be done to make it into a serious browser component. :( – Andrew Thompson Nov 06 '12 at 14:48
  • > Add HTML Content to JavaFX>You could begin about here: [Adding HTML Content to JavaFX](http://docs.oracle.com/javafx/2/webview/jfxpub-webview.htm), there Will be issues because the API has _improved_ since **2014**. Basics almost always stay-focused. Don't they?!! – will Jun 14 '16 at 15:27
  • @will: An example using `JFXPanel` is shown [here](http://stackoverflow.com/a/31576647/230513). – trashgod Jun 15 '16 at 02:12
-1

To render html content from local file, the local file's extension must be ".html". JEditor Pane will render "content.html" but will not render "content.txt".