1

I have created my RootLayout inside of JavaFX Sceene builder. It containst BorderPane and in it's center TabPane with three tabs. On first tab I want to have WebView. My RootController contains:

      @FXML
      private WebView webview;

      @FXML 
      private WebEngine webengine;

I have conected my WebView in RootLayout.fxml by it fx:id with webview variable. My initialize method in RootLayoutController is (RootLayout controler is defined in fxml file):

   @FXML
    private void initialize() {

         this.webview = new WebView();
         this.webengine = this.webview.getEngine();
         this.webengine.load("http://www.oracle.com/us/products/index.html");

    }

But page is not loading. Any suggestions ?

1 Answer 1

2

This is a duplicate of other questions, but I'm not able to find them in a reasonable amount of time searching.

Never initialize references that are injected using @FXML. Specifically, since you have

@FXML
private WebView webview ;

it is an error to then do

this.webview = new WebView();

This creates a new instance of WebView, which is distinct from the one you defined in the FXML file. Thus when you load the page you are loading it into the new WebView instance (which is not part of the scene graph).

Additionally, I doubt that you are actually creating the WebEngine directly in your FXML file. So I think you need:

@FXML
private WebView webview ;

private WebEngine webengine ;

public void initialize() {
    this.webengine = this.webview.getEngine();
    this.webengine.load("http://www.oracle.com/us/products/index.html");
}

If you really do define the webengine in FXML, then you need

@FXML
private WebView webview ;

@FXML
private WebEngine webengine ;

public void initialize() {
    this.webengine.load("http://www.oracle.com/us/products/index.html");
}
Sign up to request clarification or add additional context in comments.

1 Comment

I had the same problem, but it turns out I was just specifying "webView" for the ID of my component in the Properties > id field in Scene Builder, when I should have entered it under Code > fx:id. Rookie mistake.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.