title: JavaFX Open WebView in FXML document
body: <p>I can successfully open a WebView displaying an html document/url by itself using JavaFX WebEngine and WebView, but I want to open a WebView inside an FXML documents along with other elements such as buttons and ImageView. My goal is to have a GUI which displays images with a html document displaying inside a WebView object.</p>
  
  <p><strong>Here is the program that opens a WebView by itself:</strong></p>
  
  <pre><code>public class HTMLViewer extends Application {
  
      private Scene scene;
      MyBrowser myBrowser;
      TextArea myTextArea;
  
  
  public static void main(String[] args) throws IOException, BadLocationException {
  
      launch(args);
  }
  
      @Override
      public void start(Stage primaryStage) {
      primaryStage.setTitle("HTMLViewer");
  
      myBrowser = new MyBrowser();
      scene = new Scene(myBrowser, 800, 600);
  
      primaryStage.setScene(scene);
      primaryStage.show();
  }
  
  class MyBrowser extends Region{
  
      final String hellohtml = "chang.htm"; //html file to view
  
      WebView webView = new WebView();
      WebEngine webEngine = webView.getEngine();
  
      public MyBrowser(){
  
          URL urlHello = getClass().getResource(hellohtml); 
          webEngine.load(urlHello.toExternalForm());
  
          getChildren().add(webView);
      }
    }    
  }
  </code></pre>
  
  <p><strong>And here is my attempt and loading a WebView through an FXML document which requires a WebView.java main class, WebViewController.java, and WebView.fxml</strong></p>
  
  <p><strong>WebView.java</strong></p>
  
  <pre><code>public class WebView extends Application {
  private Object primaryStage;
  
  public static void main(String[] args) {
      launch(args);
  }
  
  @Override
  public void start(Stage primaryStage) throws IOException {
      Parent root =    FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
      primaryStage.setTitle("HTMLViewer");
      Scene scene = new Scene(root);
  
      primaryStage.setScene(scene);
      primaryStage.show();
  }
  
  
  
  WebEngine getEngine() {
      throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  }
  
  }
  </code></pre>
  
  <p><strong>WebViewController.java</strong></p>
  
  <pre><code>public class FXMLDocumentController implements Initializable {
  
  @FXML
  private Label label;
  //WebView mywebview = new WebView();
  String link = "https://www.google.com";
  WebEngine engine;
  
  @FXML
  private void handleButtonAction(ActionEvent event) {
      System.out.println("You clicked me!");
      label.setText("Hello World!");
  }
  
  @FXML WebView mywebview; //access WebView in FXML document
  public void displayWeb() {
      WebEngine engine = mywebview.getEngine();
      final String hellohtml = "chang.htm"; //HTML file to view in web view
  
      URL urlHello = getClass().getResource(hellohtml);
      engine.load(urlHello.toExternalForm());
  }
  
  @Override
  public void initialize(URL url, ResourceBundle rb) {
      engine = mywebview.getEngine();
      engine.load(link);
      displayWeb();
  }    
  
  }
  </code></pre>
  
  <p><strong>WebView.FXML</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;?import javafx.scene.web.*?&gt;
  &lt;?import java.lang.*?&gt;
  &lt;?import java.util.*?&gt;
  &lt;?import javafx.scene.*?&gt;
  &lt;?import javafx.scene.control.*?&gt;
  &lt;?import javafx.scene.layout.*?&gt;
  
  &lt;AnchorPane id="AnchorPane" prefHeight="600.0" prefWidth="900.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="webview.FXMLDocumentController"&gt;
      &lt;children&gt;
          &lt;Button fx:id="button" layoutX="23.0" layoutY="24.0" onAction="#handleButtonAction" text="Click Me!" /&gt;
          &lt;Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" /&gt;
        &lt;WebView fx:id="mywebview" layoutX="102.0" layoutY="20.0" prefHeight="559.0" prefWidth="775.0" /&gt;
      &lt;/children&gt;
  &lt;/AnchorPane&gt;
  </code></pre>
  
  <p>I get an exception while running the application</p>
  
  <p>Can not set webview.WebView field webview.WebViewController.mywebview to javafx.scene.web.WebView</p>
  
  <p>Is my goal possible to achieve? If so Does anyone know a way to do so? Any help I will be grateful for, thanks.</p>