What pure Java HTML viewers and renderers are available? The requirements are:
JComponent
interface to be placed into Scrollable
pane.I know only of a few components and projects, some of which are now defunct:
JEditorPane
, supports HTML 3.2 (as of Java 1.4)javax.swing.text.html.HTMLEditorKit
)JWebPane
(was it ever released)?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.