JavaFX Swing Browser Very Slow

2019-07-21 20:13发布

I'm trying to call something like a JFrame to authenticate a user using O-Auth. I implemented all background need for authentication, tested with default browing calling:

Desktop.getDesktop().browse(java.net.URI.create(url));

Using the default browser everythings work as it shoud. Then I tried to create my owh webview to call within my application. Looking around I found the oracle implementation Oracle JavaFX+Swing example. The Oracle implementation works but is very slow. I copied and pasted the code in eclipse and run the class under java8.

When I was giving up of Oracle example I found this one: which is faster and clean but when I call Application.launch(args); my application hangs.

public class Test extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        StackPane root = new StackPane();

            WebView view = new WebView();
        WebEngine engine = view.getEngine();
        engine.load("http://www.google.com.br/");
        root.getChildren().add(view);

        Scene scene = new Scene(root, 800, 600);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) throws IOException {
        Application.launch(args);

    }
}

If I could callApplication.launch(args); in another thread it shoud be enough.

How can I call a simple webView from my application?

1条回答
你好瞎i
2楼-- · 2019-07-21 20:51

It looks like the performance issue is a known issue with JavaFX 8u25 and JavaFX 8u40:

From my testing the issue is independent of whether the WebView is running embedded in Swing or as a stand-alone JavaFX application. The behavior is that the Oracle page will load and render completely in about six seconds if the width of the WebView is under 800 pixels and will take over thirty seconds to load if the width of the WebView is over 800 pixels. Additionally the wide WebView remains slow and unresponsive which you try to interact with it, making it unusable.

The issue is site content specific, because if you load http://www.google.com.br/ into a WebView with a size of 1024x768, it doesn't matter if the WebView is Swing wrapped or a pure JavaFX application - performance appears identical (completes loading in about 2.5seconds). Test system was a Retina Mac running OS X 10.9 and Java 8u40.

The issue is targeted to be fixed for Java 9 (you can try out early access releases here). The issue has not yet been marked as resolved for Java 9, so there is probably still ongoing work involved to fix it.

As a work-around, don't let the size of your WebView go above 800x600.

I do not know that the above is your exact issue, but it probably is. Likely it seemed as though the pure JavaFX application was performing better than the Swing based application because the pure JavaFX application was working with a smaller WebView window on (perhaps) different content.

If you continue to have issues with the actual content you need to load for your application (as opposed to something which points to oracle or google web sites), then provide a mcve which produces the issue for your content.

If your application is a Swing application, then you should use a JFXPanel to embed the WebView (like the Oracle SimpleSwingBrowser sample code) rather than trying to launch a JavaFX application from within a Swing application. If your application is a JavaFX application, then there is no need to launch the JavaFX application more than once (and the JavaFX application lifecycle rules prohibit that anyway).

查看更多
登录 后发表回答