gpt4 book ai didi

java - CheckBoxTreeTableCell 选择父事件下的所有子项

转载 作者:行者123 更新时间:2023-11-30 08:13:37 25 4
gpt4 key购买 nike

我正在尝试从父根中选择所有子复选框。当选择父复选框时,将调用该操作。

这是使用场景构建器进行的伪/修改/缩短设置:

@FXML
private TreeTableView<Info> testTable;
@FXML
private TreeTableColumn<Info, Boolean> checkBoxCol;

型号:

public class Info{
private final BooleanProperty onHold;

public Info(){
this.onHold = new SimpleBooleanProperty(false);
}
public Boolean getOnHold(){
return onHold.get();
}
public void setOnHold(Boolean onHold) {
this.onHold.set(onHold);
}
public BooleanProperty onHoldProperty(){
return onHold;
}
}

Controller :

    checkBoxCol.setCellValueFactory(new TreeItemPropertyValueFactory("onHold"));
checkBoxCol.setCellFactory(CheckBoxTreeTableCell.forTreeTableColumn(checkBoxCol));

最终结果如下所示(单击父节点时):

enter image description here

我尝试了 onEditCommit/start/cancel,但这些似乎只影响单元格而不影响复选框。我不太确定如何仅获取父节点的监听器,以便它可以检查下面的所有值(如果它们有子节点)。如果只允许父节点具有监听器太困难,那么所有复选框都可以具有监听器,我可以简单地检查是否有子节点:node.getChildren().size()

最佳答案

您应该能够在模型中完全管理它。

TreeTableViewTreeItem<Info> 组成具有一堆后代节点的根。只需安排每当您创建树项时,就向属性添加一个监听器:

private TreeItem<Info> createTreeItem(Info info) {
TreeItem<Info> item = new TreeItem<>(info);
info.onHoldProperty().addListener((obs, wasOnHold, isNowOnHold) -> {
if (isNowOnHold) {
item.getChildren().forEach(child -> child.getValue().setOnHold(true));
}
});
return item ;
}

完整示例:

import javafx.application.Application;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableView;
import javafx.scene.control.cell.CheckBoxTreeTableCell;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class TreeTableViewInheritableCheckBoxes extends Application {

@Override
public void start(Stage primaryStage) {
TreeTableView<Info> table = new TreeTableView<>();
table.setEditable(true);

TreeTableColumn<Info, Boolean> infoCol = new TreeTableColumn<>("Info");
infoCol.setPrefWidth(200);

infoCol.setCellValueFactory(cellData -> cellData.getValue().getValue().onHoldProperty());
infoCol.setCellFactory(CheckBoxTreeTableCell.forTreeTableColumn(infoCol));
table.getColumns().add(infoCol);

TreeItem<Info> root = createTreeItem(new Info());

buildTree(root, 0);

table.setRoot(root);

primaryStage.setScene(new Scene(new BorderPane(table), 250, 400));
primaryStage.show();
}

private void buildTree(TreeItem<Info> parent, int depth) {
if (depth > 2) return ;
for (int i = 0; i < 5; i++) {
TreeItem<Info> item = createTreeItem(new Info());
parent.getChildren().add(item);
buildTree(item, depth + 1);
}
}

private TreeItem<Info> createTreeItem(Info info) {
TreeItem<Info> item = new TreeItem<>(info);
info.onHoldProperty().addListener((obs, wasOnHold, isNowOnHold) -> {
if (isNowOnHold) {
item.getChildren().forEach(child -> child.getValue().setOnHold(true));
}
});
return item ;
}

public static class Info {
private final BooleanProperty onHold;

public Info(){
this.onHold = new SimpleBooleanProperty(false);
}
public Boolean getOnHold(){
return onHold.get();
}
public void setOnHold(Boolean onHold) {
this.onHold.set(onHold);
}
public BooleanProperty onHoldProperty(){
return onHold;
}
}

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

关于java - CheckBoxTreeTableCell 选择父事件下的所有子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29989892/

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