Skip to content
Advertisement

How to use FXMLLoader.load() – JavaFX 2

I am building a JavaFX application using the JavaFX Scene Builder. The interface was created in the Scene Builder and a FXML file (main.fxml) was created.

To use the interface in my application I must load the FXML file using the FXMLLoader, but there is a problem because the load() method returns an Object, and to build a Scene I need an instance of Parent class.

Below is a piece of my MainClass. The compiler is giving an error because page is not of type Parent:

JavaScript

What do I do to make this compile and run correctly?

Here is the FXML file (main.fxml):

JavaScript

Advertisement

Answer

Due to a variety of entities eligible to be loaded through FXML you need to state the expected type yourself.

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement