1

我有一个 javafx 复选框树。单击树项时,我需要选中该复选框。我为树视图的选择属性添加了一个侦听器。但是只有在单击树项时才会触发侦听器。单击复选框时,不会触发上述侦听器。

必需:在树视图中单击树项或复选框时触发的侦听器。

代码:

String memberArray = {"subChild1", "subChild2", "childSub1"}
Group groupRoot = new Group();
Scene scene = new Scene(groupRoot, Color.ALICEBLUE);
HBox hBox = new HBox();
hBox.setMaxWidth(fxPanel.getWidth());
final Label royalLabel = new Label("Select a item");

TreeSet<String> prefixMember = new TreeSet<String>();
String tmpName = null;
LinkedHashSet<CheckBoxTreeItem<String>> treeItems = new LinkedHashSet<CheckBoxTreeItem<String>>();
LinkedHashSet<CheckBoxTreeItem<String>> treeSubItems = new LinkedHashSet<CheckBoxTreeItem<String>>();

for (String item : memberArray) {
    if (!item.isEmpty()) {
        tmpName = item.substring(0, 3);
        prefixMember.add(tmpName);
    }
}

// Create and empty TreeView
TreeView<String> duckTree = new TreeView<String>();

// Create TreeItems for the Hierarchy of the TreeView
CheckBoxTreeItem<String> root = new CheckBoxTreeItem<String>("Parent");
CheckBoxTreeItem<String> lm1 = new CheckBoxTreeItem<String>("Child1");
CheckBoxTreeItem<String> lm2 = new CheckBoxTreeItem<String>("Child2");

for (String item : prefixMember) {
    CheckBoxTreeItem<String> treeItem = new CheckBoxTreeItem<String>(item.toString());
    for (String subItem : memberArray) {
        if (!subItem.isEmpty() && subItem.substring(0, 3).equals(item)) {
            CheckBoxTreeItem<String> treeSubItem = new CheckBoxTreeItem<String>(
                        subItem.toString());
                    treeSubItems.add(treeSubItem);
        }
    }
    treeItems.add(treeItem);
    treeItem.getChildren().addAll(treeSubItems);
    treeSubItems.clear();
}

root.getChildren().addAll(treeItems);
treeItems.clear();

// Create a TreeView using the root TreeItem
TreeView<String> royalTree = new TreeView<String>(root);
royalTree.setCellFactory(CheckBoxTreeCell.<String>forTreeView());

// Set a ChangeListener to handle events that occur with a Treeitem
// is selected
royalTree.getSelectionModel().selectedItemProperty()
        .addListener(new ChangeListener<TreeItem<String>>() {
            public void changed(
                    ObservableValue<? extends TreeItem<String>> observableValue,
                    TreeItem<String> oldItem, TreeItem<String> newItem) {
                // Gets fired only on selection of tree item
                // Need to get fired on selection of check box too
                // Select the respective checkbox on selection of tree item
            }
        });

hBox.getChildren().add(royalTree);
groupRoot.getChildren().add(hBox);
fxPanel.setScene(scene);
4

4 回答 4

2

您可以将 EventHandler 添加到树中的根项目中:

rootItem.addEventHandler(CheckBoxTreeItem.checkBoxSelectionChangedEvent(), new EventHandler<TreeModificationEvent<Object>>() {

            @Override
            public void handle(TreeModificationEvent<Object> event) {
                // Your code here.
            }
        });
于 2016-06-15T08:58:15.190 回答
1

我有同样的问题并搜索了很长时间。遗憾的是,oracle 没有关于此的官方文档。

答案是设置 CellFactory 并在getSelectedStateCallback().call(this.getTreeItem());
updateItem 中为您的 treeItem 调用:

// set cellFactory
royalTree.setCellFactory(new Callback<TreeView<String>, TreeCell<String>>() {
    @Override
    public TreeCell<String> call(TreeView<String> p) {
        // return new CheckBoxTreeCell, you also can make a new class with this
        return new CheckBoxTreeCell<String>() {
            @Override
            public void updateItem(String item, boolean empty) {
                super.updateItem(item, empty);
                if (!empty) {
                    // call the selectedStat Callback for treeitem
                    ObservableValue<Boolean> selectedState = getSelectedStateCallback().call(this.getTreeItem());
                    if (selectedState != null) {
                        // do something here
                    }
                }
            }
        };
    }
});

我已经在 fx 8 中对此进行了测试,但它也应该在 fx 2.2 中工作

快乐编码,
kalasch

于 2014-03-28T11:53:18.013 回答
1

是的,向树项添加事件处理程序是可行的。下面是一些示例代码 (Java 8),其中包含带有 CheckBoxTreeItem 项的 TreeView:

CheckBoxTreeItem<Path> rootItem = new CheckBoxTreeItem<>(rootDirPath);
rootItem.addEventHandler(
        CheckBoxTreeItem.<Path>checkBoxSelectionChangedEvent(), 
        (TreeModificationEvent<Path> e) -> {
    CheckBoxTreeItem<Path> item = e.getTreeItem();  
    if (item.isSelected() || item.isIndeterminate()) {
        System.out.println("Some items are checked");
    }
    else {
        System.out.println("Some items are unchecked");
    }       
});
TreeView<Path> tree = new TreeView<>(rootItem);
tree.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
于 2017-05-03T07:03:58.063 回答
0

您是否需要立即为每个选择进行事件?如果不是,您可以创建一个包含所有复选框树项的数组列表,并在需要时对其进行迭代以检查选中或未选中。

于 2013-12-16T20:46:05.070 回答