我需要在应用程序启动时加载JavaFX TreeView数据。问题是节点查找()方法总是返回null。
码
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();
}
}http://pastebin.com/aC2gHMjp
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)我已经找到了this解决方案,但对我来说(两者都不起作用)。
有人能帮我解决这个问题吗?
发布于 2014-08-27 16:16:00
lookup(...)通常会返回null,直到CSS通过为止,这通常意味着您需要在显示阶段之后(但可能在此之后)执行。
但这并不是做你想做的事的正确方法。定义控制器,在fx:id上设置TreeView,并在控制器的initialize()方法中执行初始化。
发布于 2022-08-19 23:08:01
根据我的经验,JavaFX拆分窗格的结构与Pane类的其他子类不同。我遇到了这个问题,我是如何修正的--调用getItems()来获取一个应该是容器类型的拆分窗格的项,例如锚窗格、堆栈等等--然后在那里执行查找方法,它工作得很好。
FXMLLoader pageLoader = new FXMLLoader(LoginPageController.class.getResource("FriendListPage.fxml"));
SplitPane friendListPage = (SplitPane) pageLoader.load();
AnchorPane addFriendSubWindow = (AnchorPane) friendListPage.getItems().get(0);
Button btnCloseFriendWindow = (Button) addFriendSubWindow.lookup("#btnCloseFriendWindow");
btnCloseFriendWindow.setOnAction(event -> {
if(!extraPanelContainer.getChildren().isEmpty())
extraPanelContainer.getChildren().clear();
clearFriendList(friendListPage);
});btnCloseFriendWindow.setOnAction()用于返回null,直到我使用这种方法。
https://stackoverflow.com/questions/25531743
复制相似问题