JavaFX2.0 webview not rendering the page?

2019-02-20 15:25发布

问题:

I have written the following code in JavaFX2.0

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

/**
 *
 * @author roger
 */
public class WebViewDemo extends Application {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        StackPane root = new StackPane();
        WebView x = new WebView();
        WebEngine ex = x.getEngine();
        ex.load("http://www.google.com");
        root.getChildren().add(x);
        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

I am getting the following error and am not able to figure out what went wrong ?

Jul 03, 2012 11:18:42 PM com.sun.webpane.webkit.network.URLLoader doRun
WARNING: Unexpected error
java.lang.IllegalArgumentException: Illegal cookie name
    at java.net.HttpCookie.<init>(HttpCookie.java:158)
    at java.net.HttpCookie.parseInternal(HttpCookie.java:964)
    at java.net.HttpCookie.parse(HttpCookie.java:215)
    at java.net.HttpCookie.access$100(HttpCookie.java:58)
    at java.net.HttpCookie$12.parse(HttpCookie.java:1096)
    at sun.net.www.protocol.http.HttpURLConnection.filterHeaderField(HttpURLConnection.java:2605)
    at sun.net.www.protocol.http.HttpURLConnection.getFilteredHeaderFields(HttpURLConnection.java:2642)
    at sun.net.www.protocol.http.HttpURLConnection.getHeaderFields(HttpURLConnection.java:2686)
    at com.sun.webpane.webkit.network.URLLoader.extractHeaders(URLLoader.java:776)
    at com.sun.webpane.webkit.network.URLLoader.willSendRequest(URLLoader.java:574)
    at com.sun.webpane.webkit.network.URLLoader.doRun(URLLoader.java:155)
    at com.sun.webpane.webkit.network.URLLoader.access$000(URLLoader.java:42)
    at com.sun.webpane.webkit.network.URLLoader$1.run(URLLoader.java:104)
    at com.sun.webpane.webkit.network.URLLoader$1.run(URLLoader.java:101)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.webpane.webkit.network.URLLoader.run(URLLoader.java:101)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)

please let me know what needs to be modified and what went wrong ?

回答1:

Some google-fu I found this forum post that led to this bug RT-15676. The post suggested adding the line:

java.net.CookieHandler.setDefault(null);

after the application starts but before starting the the WebView. So I placed it just before adding the root StackPane to the scene. And it seems to work now.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class WebViewDemo extends Application {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        StackPane root = new StackPane();
        WebView x = new WebView();
        WebEngine ex = x.getEngine();
        ex.load("http://www.google.com");

        root.getChildren().add(x);
        java.net.CookieHandler.setDefault(null);
        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

}


标签: java javafx-2