JavaFX CheckBoxTreeItem选择

时间:2013-09-25 04:21:53

标签: java checkbox treeview javafx

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

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

代码:

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 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

您只需将EventHandler添加到树中的根项目:

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

            @Override
            public void handle(TreeModificationEvent<Object> event) {
                // Your code here.
            }
        });

答案 2 :(得分:1)

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

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);

答案 3 :(得分:0)

您是否需要立即为每个选项举办活动?如果没有,你可以创建所有checkboxtreeitems的arraylist,并在需要时迭代它以检查选中或未选中。