user
user

Reputation: 158

Initializing a WebView created using SceneBuilder

I am just getting started with JavaFX and have created a simple gui in SceneBuilder and can get most function to work apart from WebView.

In my FXML code I have:

<WebView id="webComponent" prefHeight="200.0" prefWidth="200.0" />

Then in my controller class I have the following code that is run when a button is pressed:

webComponent.getEngine().load("http://google.com/");

When I try to run the program when I press the button, it first prints a string to the console (I programmed it to do this), but then throws a null pointer exception and the WebView remains blank.

Could someone shed some light on this as it has me stumped at the moment.

Thanks

Upvotes: 0

Views: 1361

Answers (1)

Jos&#233; Pereda
Jos&#233; Pereda

Reputation: 45476

If you are dealing with FXML files, and you want to use your @FXML annotated components in the Controller class, you need to provide an id with fx:id.

In your case:

<WebView fx:id="webComponent" prefHeight="200.0" prefWidth="200.0" />

Usually, id is for CSS settings.

Check the documentation here.

Upvotes: 2

Related Questions