JavaFX 2.0 + FXML - strange lookup behaviour

前端 未结 2 2001
南方客
南方客 2020-12-11 06:21

I want to find a VBox node in a scene loaded with FXMLoader thanks to Node#lookup() but I get the following exception :

java.lang.Cla

2条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-11 06:44

    The easiest way to get a reference to the VBox is by calling FXMLLoader#getNamespace(). For example:

    VBox myvbox = (VBox)fxmlLoader.getNamespace().get("myvbox");
    

    Note that you'll need to create an instance of FXMLLoader and call the non-static version of load() in order for this to work:

    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("test.fxml"));
    AnchorPane page = (AnchorPane) fxmlLoader.load();
    

提交回复
热议问题