我有一个包含TreeView控件的FXML文件:
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="500.0" xmlns:fx="http://javafx.com/fxml" fx:controller="test.MyControllerClass">
<TreeView fx:id="locationTreeView" layoutX="12.0" layoutY="158.0" prefHeight="193.0" prefWidth="471.0" />
然后我的Java类控制器需要用这个TreeView包装起来,并动态地添加TreeItem。 问题在于,它没有加载那些TreeItem。下面是我的控制器中的测试代码:
public class MyControllerClass extends Application {
@FXML
private TreeView<String> locationTreeView;
@Override
public void start(Stage stage) throws Exception {
stage.initStyle(StageStyle.TRANSPARENT);
stage.getIcons().add(new Image(getClass().getResourceAsStream("myIcon.png")));
Parent root = FXMLLoader.load(getClass().getResource("myInterface.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
loadTreeItems();
stage.show();
}
// Just a simple example that still doesn't works
private void loadTreeItems() {
try {
TreeItem<String> root = new TreeItem<String>("Root Node");
root.setExpanded(true);
root.getChildren().addAll(
new TreeItem<String>("Item 1"),
new TreeItem<String>("Item 2"),
new TreeItem<String>("Item 3")
);
locationTreeView = new TreeView<String>(root);
} catch (Exception exc) {
System.out.println("Error: " + exc.getMessage());
}
}
public static void main(String[] args) {
launch(args);
}
}
有什么想法为什么它不起作用?