开发者

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

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 2 years ago.

Improve this question

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 b开发者_如何学编程e 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)


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.


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.


CSSBox might be what you're looking for: http://cssbox.sourceforge.net


Check out this article: http://devdaily.com/blog/post/jfc-swing/how-create-simple-swing-html-viewer-browser-java

It uses JEditorPane and some other Swing classes to parse and render not only HTML, but also CSS.


You can also access the native browser through something like: http://djproject.sourceforge.net/ns/

For certain web pages, this is sometimes the only way to go. There are always trade offs.

I have yet to find a browser component that renders well, is open source, and sufficiently flexible at the same time. Cobra comes close but there are pages that it won't render and it's tough (impossible?) to do things like get rid of its own scroll bars, etc..


Wow haferblues, I never thought I would find something I like about JavaFX. But the browser implementation is really nice. For those (like me) that never have used JavaFx before here the complete class (for the snippet of haferblues):

import com.sun.javafx.application.PlatformImpl;

import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;

public class SwingBrowser extends JFXPanel {
    private static final long serialVersionUID = 1L;

    public SwingBrowser(String url) {
        PlatformImpl.startup(new Runnable() {
            @Override
            public void run() {
                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(url);

                setScene(scene);
            }
        });
    }
}


The Flying Saucer was doing the job OK, but the following rendered text example was a huge turnoff for my mobile app development on Linux Java :

Sometimes the period at the end changes line without the text beside .

Also, the text isn't selectable unlike for the JTextPanel.

Parser only seems to accept UTF-8 encoding. I couldn't manage to force my own encoding when parsing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜