Issue
I need to load JavaFX TreeView data at application startup. The problem is node lookup() method always return null.
CODE
public class Main extends Application {
public static final String APPLICATION_NAME = "testapp" ;
public static void main(String args[]) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
String fxml = "/fxml/main-window.fxml";
String globalCss = "/styles/main.css";
FXMLLoader loader = new FXMLLoader(getClass().getResource(fxml));
Parent root = (Parent) loader.load();
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.getScene().getStylesheets().add(globalCss);
primaryStage.setMaximized(true);
TreeView treevw = (TreeView) scene.lookup("#treevw");
// lazy init here
primaryStage.show();
}
}
FXML
java version "1.8.0_20"
Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)
i've found this solution but it doesn't work for me (both).
Can anybody please help to solve the issue?
Solution
lookup(...)
will generally return null until a CSS pass has been made, which usually means you need to do it after showing the stage (but possibly after that).
But this is really not the right way to do what you're trying to do. Define a controller, set an fx:id
on your TreeView
and perform the initialization in the controller's initialize()
method.
Answered By - James_D
Answer Checked By - Candace Johnson (JavaFixing Volunteer)