gpt4 book ai didi

java - 如何使用 JavaFX 将 TreeView 插入到 Accordion 中

转载 作者:行者123 更新时间:2023-12-02 07:04:46 24 4
gpt4 key购买 nike

我有一个问题,如何插入可扩展到 Accordion 的 Threeview。

这是 Accordion 的代码:

public TitledPane createConnectionsTree(String title) {
connectionsData = FXCollections.observableArrayList(connectionsList);
ListView<ConnectionsObject> lv = new ListView<>(connectionsData);

lv.setCellFactory(new Callback<ListView<ConnectionsObject>, ListCell<ConnectionsObject>>() {
@Override
public ListCell<ConnectionsObject> call(ListView<ConnectionsObject> p) {
return new ConnectionsCellFactory();
}
});
AnchorPane content = new AnchorPane();
content.getChildren().add(lv);
// add to TitelPane
TitledPane pane = new TitledPane(title, content);
return pane;
}

这是 TreeView 代码:

public void initTree() {
rootNode.setExpanded(true);

for (Employee employee : employees) {
TreeItem<String> empLeaf = new TreeItem<>(employee.getName());
boolean found = false;

for (TreeItem<String> depNode : rootNode.getChildren()) {
if (depNode.getValue().contentEquals(employee.getDepartment())) {
depNode.getChildren().add(empLeaf);
found = true;
break;
}
}
if (!found) {
TreeItem depNode = new TreeItem(employee.getDepartment());
rootNode.getChildren().add(depNode);
depNode.getChildren().add(empLeaf);
}
}
VBox box = new VBox();
TreeView<String> treeView = new TreeView<>(rootNode);
treeView.setShowRoot(true);
treeView.setEditable(true);
box.getChildren().add(treeView);
}

附注

我得到这个结果:

enter image description here

我希望当我伸展树(Splay Tree) View 时展开 Accordion 的 slider 而不是 TreeView 的 slider 。这是我测试过的代码:

 public TitledPane createConnectionsList(String title) {

rootNode.setExpanded(true);
for (ThreeData conn : connectionsThree) {
TreeItem<String> empLeaf = new TreeItem<>(conn.getName());
boolean found = false;
for (TreeItem<String> depNode : rootNode.getChildren()) {
if (depNode.getValue().contentEquals(conn.getDepartment())) {
depNode.getChildren().add(empLeaf);
found = true;
break;
}
}
if (!found) {
TreeItem depNode = new TreeItem(conn.getDepartment());
rootNode.getChildren().add(depNode);
depNode.getChildren().add(empLeaf);
}
}
TreeView<String> treeView = new TreeView<>(rootNode);
treeView.setShowRoot(true);
treeView.setEditable(true);

AnchorPane content = new AnchorPane();
// Set aligment - fill the accordion with the three content
AnchorPane.setLeftAnchor(treeView, 0d);
AnchorPane.setRightAnchor(treeView, 0d);
AnchorPane.setBottomAnchor(treeView, 0d);
AnchorPane.setTopAnchor(treeView, 0d);

content.getChildren().add(treeView);
// Add to TitelPane
TitledPane pane = new TitledPane(title, content);
return pane;
}

最佳答案

当您使用 FXML 在 SceneBuilder 中设计 GUI 时,问题可能会更加明显。您需要使用 AnchorPane 锚定节点,以便它们拉伸(stretch)。例如:

    AnchorPane.setLeftAnchor(aNode, 0d);
AnchorPane.setRightAnchor(aNode, 0d);
AnchorPane.setBottomAnchor(aNode, 0d);
AnchorPane.setTopAnchor(aNode, 0d);

这会将节点 aNode 的每个角锚定到 AnchorPane 的角上。这是在 AnchorPane 上选择“适合父级”选项时在 SceneBuilder 中得到的结果。

如果做不到这一点,只需使用 FXML,这将更容易获得您想要的 GUI。

关于java - 如何使用 JavaFX 将 TreeView 插入到 Accordion 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16235171/

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