gpt4 book ai didi

JavaFX:向嵌套 CheckBoxTreeItem[s] 的复选框添加 CTRL 单击功能

转载 作者:行者123 更新时间:2023-11-30 06:55:00 27 4
gpt4 key购买 nike

我有一种情况,正在显示一个 TreeView,有两个级别的条目(父级和子级),如下所示:

root (invisible)
|_ parent item 1
|_ child item 1-1
|_ child item 1-2
|_ parent item 2
|_ child item 2-1

这些项目都是标准的CheckBoxTreeItem。我想要做的是,根据某些功能,按住 CTRL 键单击父项的复选框来选择一组子项。例如,在这里我可能只想在按 CTRL- 时选择每个子列表中的第一个子项目(即 child item 1-1child item 2-1)单击父复选框。

这可能吗? 据我所知,没有很好的方法来访问该复选框并给出例如onMouseClick 事件处理程序,这对我来说是有意义的解决方案。

上面给出的示例树布局的代码:

TreeViewTest.java

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.CheckBoxTreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.control.cell.CheckBoxTreeCell;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class TreeViewTest extends Application {

@Override
public void start(final Stage stage) {
StackPane sceneRoot = new StackPane();

// create the tree model
CheckBoxTreeItem<String> parent1 = new CheckBoxTreeItem<>("parent 1");
CheckBoxTreeItem<String> parent2 = new CheckBoxTreeItem<>("parent 2");
CheckBoxTreeItem<String> child1_1 = new CheckBoxTreeItem<>("child 1-1");
CheckBoxTreeItem<String> child1_2 = new CheckBoxTreeItem<>("child 1-2");
CheckBoxTreeItem<String> child2_1 = new CheckBoxTreeItem<>("child 2-1");
CheckBoxTreeItem<String> root = new CheckBoxTreeItem<>("root");

// attach the nodes
parent1.getChildren().addAll(child1_1, child1_2);
parent2.getChildren().addAll(child2_1);
root.getChildren().addAll(parent1, parent2);

// display everything
root.setExpanded(true);
parent1.setExpanded(true);
parent2.setExpanded(true);

// create the treeView
final TreeView<String> treeView = new TreeView<>();
treeView.setShowRoot(false);
treeView.setRoot(root);

// set the cell factory
treeView.setCellFactory(CheckBoxTreeCell.forTreeView());

// display the tree
sceneRoot.getChildren().addAll(treeView);
Scene scene = new Scene(sceneRoot, 200, 200);
stage.setScene(scene);
stage.show();
}

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

最佳答案

您需要 TreeCell 的自定义实现。这应该为您提供一个起点,允许您实现所需的附加功能:

public class MyCheckBoxCell extends TreeCell<String> {

private final CheckBox checkBox = new CheckBox();

private BooleanProperty currentSelectedBinding ;

// only need this if you are using the indeterminateProperty() of your
// CheckBoxTreeItems
private BooleanProperty currentIndeterminateBinding ;

public MyCheckBoxCell() {

// add extra event handling to the check box here...

}

@Override
protected void updateItem(String item, boolean empty) {

super.updateItem(item, empty);

if (empty) {
setText(null);
setGraphic(null);
} else {
setText(item);
setGraphic(checkBox);
if (currentSelectedBinding != null) {
checkBox.selectedProperty().unbindBidirectional(currentSelectedBinding);
}
if (currentIndeterminateBinding != null) {
checkBox.indeterminateProperty().unbindBidirectional(currentIndeterminateBinding);
}
if (getTreeItem() instanceof CheckBoxTreeItem) {
CheckBoxTreeItem cbti = (CheckBoxTreeItem<?>) getTreeItem();
currentSelectedBinding = cbti.selectedProperty();
checkBox.selectedProperty().bindBidirectional(currentSelectedBinding);
currentIndeterminateBinding = cbti.indeterminateProperty();
checkBox.indeterminateProperty().bindBidirectional(currentIndeterminateBinding);
}
}
}
}

关于JavaFX:向嵌套 CheckBoxTreeItem[s] 的复选框添加 CTRL 单击功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42024442/

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