Search code examples
javajavafxwebviewsandboxsigning

JavaFX FXML: Blank WebView After Load


When I try to load any html or urls using webEngine.load(); my webView is just blank. From what I have read here "JavaFX 2.2 WebView" it seems like I will have to sign my application to let it run outside of sandbox mode. http://docs.oracle.com/javafx/2/deployment/deploy_overview.htm#CEGJGHDA

Is that what is causing this problem?

I am using NetBeans 8.1, and under Project Settings I am running it as Standalone. I have been following these tutorials and each has gone well. http://docs.oracle.com/javase/8/javafx/get-started-tutorial/get_start_apps.htm#JFXST804

Here are my three files.

FXML

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.media.*?>
<?import javafx.scene.web.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.text.*?>
<?import javafx.geometry.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="481.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <AnchorPane prefHeight="18.0" prefWidth="600.0">
         <children>
            <HBox layoutX="157.0" layoutY="14.0" prefHeight="64.0" prefWidth="287.0">
               <children>
                  <Label text="TwitchAid">
                     <font>
                        <Font size="53.0" />
                     </font>
                  </Label>
                  <ImageView fitHeight="150.0" fitWidth="38.0" pickOnBounds="true" preserveRatio="true">
                     <image>
                        <Image url="@Twitchaid-Logo.png" />
                     </image>
                  </ImageView>
               </children>
            </HBox>
         </children>
      </AnchorPane>
      <AnchorPane prefHeight="200.0" prefWidth="200.0">
         <children>
            <WebView fx:id="webView" prefHeight="405.0" prefWidth="600.0" />
         </children>
      </AnchorPane>
   </children>
</VBox>

Java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package twitchauthorize;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author Dylan
 */
public class TwitchAuthorize extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLTwitchAuthorize.fxml"));
        Scene scene = new Scene(root);   
        stage.setScene(scene);
        stage.show();

        stage.setResizable(false);
    }

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

}

Controller.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package twitchauthorize;

import javafx.fxml.FXML;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;

/**
 *
 * @author Dylan
 */
public class FXMLTwitchAuthorizeController {

    @FXML
    private WebView webView;

    @FXML
    private void initialize(){
        WebEngine engine = webView.getEngine();
        engine.load("http://www.google.com");
    }

}

Solution

  • You haven't specified a controller in your FXML, so the initialize method of the controller is never executed.

    Add the following attribute definition to the VBox element that forms the root element of your FXML:

    fx:controller="twitchauthorize.FXMLTwitchAuthorizeController"