java – CheckedTreeSelectionDialog最初检查元素

我正在使用CheckedTreeSelectionDialog,我想最初选择一些项目.

如何使用setInitialSelections方法选择子级(level2项)而不是level1.

CheckedTreeSelectionDialog dialog = new CheckedTreeSelectionDialog(
    this.containerComposite.getShell(), new myLabelProvider(), new
    myContentProvider());

dialog.setContainerMode(true);
dialog.setInput(new MyModel());

Parent p = new Parent("I am a parent");
p.getChildren.add(new Child("I am a child"));
dialog.setInitialSelection(p);

当containerMode为false时,未选中子项,如果为true,则选择所有子项时为子项.

最佳答案 只需使用方法
SelectionDialog#setInitialElementSelections(List elements)并在列表中传递要选择的元素:

CheckedTreeSelectionDialog dialog = new CheckedTreeSelectionDialog(
this.containerComposite.getShell(), new myLabelProvider(), new myContentProvider());

dialog.setContainerMode(true);
dialog.setInput(new MyModel());

List<Child> list = new ArrayList<Child>();

/* fill your list */

dialog.setInitialElementSelections(list);
点赞