Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pure Java HTML viewer/renderer for use in a Scrollable pane [closed]

What pure Java HTML viewers and renderers are available? The requirements are:

  • It should implement the JComponent interface to be placed into Scrollable pane.
  • It should be preferably a free solution; open source is a plus.
  • Its availability as Maven artifact is a plus.

I know only of a few components and projects, some of which are now defunct:

  • Built-in JEditorPane, supports HTML 3.2 (as of Java 1.4)
  • DJ Project (it is pure Java?)
  • Ekit by hexidec (is based on javax.swing.text.html.HTMLEditorKit)
  • JSyndrome HTML Editor by Sferyx
  • JWebPane (was it ever released)?
  • JDIC (abandoned; from some info here I see that it is native)
  • (PDF renderer) WebRenderer (former XHTMLRenderer)
like image 913
dma_k Avatar asked Mar 13 '10 11:03

dma_k


2 Answers

Since Java 8, you can use JavaFX's WebView Component, which can also be used in Swing.

Code is as simple as:

JFXPanel jfxPanel = new JFXPanel(); // Scrollable JCompenent Platform.runLater( () -> { // FX components need to be managed by JavaFX    WebView webView = new WebView();    webView.getEngine().loadContent( "<html> Hello World!" );    webView.getEngine().load( "http://www.stackoverflow.com/" );    jfxPanel.setScene( new Scene( webView ) ); }); 

It is backed by the WebKit engine (version depends on JRE and is reasonably up to date). But keep in mind that it is not a full browser, so don't count on support of, say, HTML5 audio/video. Otherwise, it runs HTML + CSS + JS as good as your browser.

Technically, the underlying engine is C++, not native Java. But it is bundled in Oracle's official JRE, requires no library, has zero config, is as cross-platform as Java FX, and is actively updated and maintained.

As good as native Java for most use cases, I think?


The information below is outdated, seeing that we now have WebView in Java.

Tried Cobra/Lobo, CSSBox, and Flying Saucer, all pure Java. Others are either native or commercial.

Content: Simple HTML generated on the fly (as string), embedded CSS 2.1, no JS.

Short story: Flying Saucer is simplest to use and render is most correct, but you better have full control over content. Otherwise look for a native solution.

Long story:

CSSBox seems to be more active, however it seems to depends on some 3rd party libraries. For example the demo depends on nekohtml which use apache xerces which changed the way the default Java 1.7 sax parser works and broke my program, but when I force it to use java's built in xerces I get ClassCastException (InlineBox to BlockBox). Can't get it to work at the end. Plus still haven't found a way to replace the document in an existing BrowserCanvas.

Cobra is no longer maintained, have to manually fix an incompatibility issue to make it works in 1.7. Also need to grab mozilla Rhino (not using any JS) but that is all. After that it is fairly smooth, just need to ask Logger to hide paint messages. Render is correct and speed is fair - as long as the document is simple. When you start to use less common tags or more complicated layout, Cobra falls apart pretty quickly.

Flying Saucer has the best CSS support of the three as of writing (Feb 2011). Setup is very easy (e.g. no need to setup document like cobo or domparser like cssbox) has few dependency - which also means no javascript. But Flying Saucer is very strict about what you feed it. The source must be a well-formed XML, for example style and script may have to be wrapped in CDATA and if you use html entities you must declare DTD (so no html5 doctype). However if you are embedding content that you can control then it may be your best choice.

like image 139
5 revs Avatar answered Sep 22 '22 21:09

5 revs


If you are using Swing, you can embed a JavaFX WebView.

1)Should implement JComponent interface to be placed into Scrollable pane.

In order to add the WebView to Swing you need to add it to JFXPanel, which is a JComponent. To make the WebView fill the full JFXPanel, I used an AnchorPane like so:

                final AnchorPane anchorPane = new AnchorPane();                 WebView webBrowser = new WebView();                  //Set Layout Constraint                 AnchorPane.setTopAnchor(webBrowser, 0.0);                 AnchorPane.setBottomAnchor(webBrowser, 0.0);                 AnchorPane.setLeftAnchor(webBrowser, 0.0);                 AnchorPane.setRightAnchor(webBrowser, 0.0);                  //Add WebView to AnchorPane                 anchorPane.getChildren().add(webBrowser);                  //Create Scene                 final Scene scene = new Scene(anchorPane);                  // Obtain the webEngine to navigate                 final WebEngine webEngine = webBrowser.getEngine();                 webEngine.load("http://www.google.com");                 _jfxPanel.setScene(scene); 

Whenever you run JavaFX code, make sure to run it in Platform.runLater().

2) Should be preferably a free solution; opensource is a plus.

Well, it's pure Oracle java.

3) Availability as maven artifact is a plus.

See the StackOverflow answer Maven project with JavaFX (with jar file in `lib`) for advice on integrating JavaFX and Maven.

From Java8 on JavaFX will be fully integrated in Java.

Additonal Pros: -supports HTML5 and JavaScript (uses webkit) -supports platform interoperability -even supports interacting with the DOM, run JavaScript, get notified of events from the Webview.

Cons: -JavaFX needs to be installed. But it comes bundled with java since v7u6 (August 2012).

Other experiences:

I tried djproject, but had lots of problems with platform interoperability. Worked quite well on Windows, but only with major effort on Linux and I couldn't get it to work on Mac. For every platform you also need to build a 32bit and 64bit version of your jar. With lot of effort and a huge jar file you could possibly merge everything together in one jar. But this was far from being convenient.

Compared to the JavaFX solution I mentioned above, the DJProject was a way bigger pain.

like image 30
haferblues Avatar answered Sep 22 '22 21:09

haferblues