gpt4 book ai didi

java - 如何通过代码初始化JavaFX fxml文件中定义的树形控件

转载 作者:行者123 更新时间:2023-12-01 18:48:43 24 4
gpt4 key购买 nike

我想要实现的效果如下,都是代码创建的。 enter image description here

代码如下,这个方法可以达到我想要的结果,因为在start方法中,先初始化TreeView,然后创建场景,添加进去,然后显示成功。示例1:

public class treeViewDemo_4 extends Application {

// Node图标
private final Node rootIcon = new ImageView(
new Image(getClass().getResourceAsStream("/icon/file.png"))
);

private TreeView<String> createTreeView(TreeItem<String> root1,TreeItem<String> root2) {
TreeItem<String> dummyRoot = new TreeItem<>();
dummyRoot.getChildren().addAll(root1,root2);
TreeView<String> tree = new TreeView<>(dummyRoot);
tree.setShowRoot(false);
return tree ;
}

final TreeItem<String> root1 = new TreeItem<>("root 1");
final TreeItem<String> root2 = new TreeItem<>("root 2");

public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Tree View Sample");
// 每个Item下又可以添加新的Item
for (int i = 1; i < 6; i++) {
TreeItem<String> item = new TreeItem<> ("Message" + i);
item.getChildren().add(new TreeItem<String>("第三级"));
root1.getChildren().add(item);
}
// 创建TreeView
TreeView<String> tree = createTreeView(root1,root2);

StackPane root = new StackPane();
root.getChildren().add(tree);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
}

但是我想通过fxml文件来实现这个效果,但是不知道怎么做。首先是我的代码,Example2:

fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TreeView?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane fx:controller="com.fendo.flying.demo.treeview.demo5.demo5Controller" fx:id="rootPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="705.0" prefWidth="1113.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" >
<children>
<TreeView fx:id="treeView" layoutX="3.0" layoutY="94.0" prefHeight="608.0" prefWidth="214.0">

</TreeView>
<Button layoutX="300.0" layoutY="93.0" mnemonicParsing="false" text="Button" />
<Button layoutX="715.0" layoutY="93.0" mnemonicParsing="false" text="Button" />
<Button layoutX="597.0" layoutY="93.0" mnemonicParsing="false" text="Button" />
<Button layoutX="393.0" layoutY="93.0" mnemonicParsing="false" text="Button" />
<Button layoutX="492.0" layoutY="93.0" mnemonicParsing="false" text="Button" />
</children>
</AnchorPane>

Controller


public class demo5Controller implements Initializable {

@FXML
private TreeView treeView;

final TreeItem<String> root1 = new TreeItem<>("root 1");
final TreeItem<String> root2 = new TreeItem<>("root 2");
final TreeView<String> tree = createTreeView(root1,root2);

private TreeView<String> createTreeView(TreeItem<String> root1,TreeItem<String> root2) {
TreeItem<String> dummyRoot = new TreeItem<>();
dummyRoot.getChildren().addAll(root1,root2);
TreeView<String> tree = new TreeView<>(dummyRoot);
tree.setShowRoot(false);
return tree ;
}


public demo5Controller() {
treeView = createTreeView(root1,root2);
}

@Override
public void initialize(URL location, ResourceBundle resources) {
treeView = createTreeView(root1,root2);
}
}

示例2 运行后无效。 Treeview没有初始化成功,显示为空,我想知道如何通过代码初始化fxml中定义的TreeView?

或者,我在 fxml 中定义了一个 TreeView 。如何在 Controller 中初始化或覆盖它?实现示例1的效果

示例2中如何实现,即示例1中以下代码的效果:

// create TreeView
TreeView<String> tree = createTreeView(root1,root2);

StackPane root = new StackPane();
root.getChildren().add(tree);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();

最佳答案

要完全通过 fxml 创建和配置 TreeView,请在 <TreeView> 中添加 treeItem 结构。标签,基本上类似于:

<TreeView fx:id="treeView"> 
<TreeItem value = "treeRoot" expanded="true">
<children>
<TreeItem value="firstChild" />
<TreeItem value="secondChild" />
</children>
</TreeItem>
</TreeView>

要进行更深层次的嵌套,请对子项执行相同的操作。

要通过 fxml 创建 TreeView 并通过代码配置其内容,请在 Controller 的初始化中执行后者(重要:不要手动实例化 TreeView!)

@FXML private TreeView<String> treeView;

@FXML
private void initialize() {
treeView.setRoot(createItemHierarchy(new TreeItem<String>("root1"), new TreeItem<String>("root2")));
}

private TreeItem<String> createItemHierarchy(TreeItem<String>... root1) {
TreeItem<String> root = new TreeItem<>("base root");
root.getChildren().addAll(root1);
return root ;
}

关于java - 如何通过代码初始化JavaFX fxml文件中定义的树形控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59771211/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com