Scene Builder and <fx:root>

Werner Lehmann lehmann at
Thu Jul 12 08:32:42 PDT 2012


today I tested the setRoot method on FXMLLoader, introduced with 2.2. 
After some trial and error (the docs do not seem to be updated) this 
seems to work fine, at least for my simple test. See below for some 

However, I am wondering how this is intended to integrate with Scene 
Builder. Maybe as follows?

1. Create some FXML in scene builder.

2. Load in IDE and manually wrap root element (e.g. BorderPane) into a 
new <fx:root> element with namespace and type attribute. This will of 
course correspond to the pre-instantiated root class of that type, as 
opposed to an FXMLLoader instantiated controller.

3. Continue editing in scene builder.

The last step does not work currently, using the latest FX 2.2 and Scene 
Builder versions. Scene Builder will throw NullPointerException on the 
line of the added <fx:root> element. I am guessing it does not know the 
class specified in the type attribute. This prevents further work the 
FXML in Scene Builder.

I suppose Scene Builder could safely ignore that type and just continue 
to work with the <fx:root> child element (starting with BorderPane in my 

Otherwise, how shall I inform Scene Builder about my root class? The 
same question presents itself also for custom controls which are to be 
used somewhere in the FXML - so Scene Builder must know about them.


> public class MintBackgroundJobsDialog extends BorderPane
> {
>   @FXML private Button bClearList;
>   public MintBackgroundJobsDialog() throws IOException
>   {
>     FXMLLoader loader = new FXMLLoader();
>     loader.setRoot(this);
>     loader.setController(this);
>     Object root = loader.load(getClass().getResource("MintBackgroundJobsDialog.fxml").openStream());
>     System.out.println(root == this);
>     System.out.println(bClearList.getText());
>   }
>   public static void main(String[] args) throws IOException
>   {
>     new MintBackgroundJobsDialog();
>   }
>   @FXML
>   protected void clearListClicked() {}
> }


> <fx:root xmlns:fx="" type="">
> <BorderPane id="BorderPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0">
>   <bottom>
>     <HBox id="" fillHeight="false">
> </BorderPane>
> </fx:root>

More information about the openjfx-dev mailing list