首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用CheckBoxTreeSelectionModel为JIDE CheckBoxTree使用setSelectionPaths()?

如何使用CheckBoxTreeSelectionModel为JIDE CheckBoxTree使用setSelectionPaths()?
EN

Stack Overflow用户
提问于 2011-05-12 01:37:58
回答 1查看 1.9K关注 0票数 0

我使用了CheckBoxTree类,它是JIDE公共层包(http://jidesoft.com/products/oss.htm)的一部分。我希望能够做的是保存和加载CheckBoxTreeSelectionModel的状态,这是什么跟踪哪些框被选中或没有。我可以通过保存selectionModel.getSelectionPaths()来保存它,但是我的问题是加载它。当我使用selectionModel.setSelectionPaths()时,它只检查路径的根和叶的复选框,而不检查它们之间的方框。奇怪的是,当我保存getSelectionPaths()的结果,然后将其直接提供给setSelectionPaths()时,也会发生这种情况。

对于FileSystemModel,我使用了一些我发现的代码,它们喜欢使用文件对象而不是TreeNodes。我尝试了在网上不同地方找到的FileSystemModels和CheckBoxTrees的不同组合,结果总是相同的。我大概在这个问题上花了将近20个小时……承认这一点有点尴尬。如有任何帮助,我们不胜感激!

我的代码如下。这将创建CheckBoxTree并尝试加载"/Documents和设置/管理员“,这将导致"/”和“管理员”及其所有子项被选中,而不是“文档和设置”。

代码语言:javascript
复制
public class CheckBoxTreeFrame {
    private FileSystemModel fileSystemModel = null;
    private CheckBoxTree checkBoxTree = null;
    private JFrame main_frame = null;
    private CheckBoxTreeSelectionModel selectionModel = null;

    public CheckBoxTreeFrame(){
        // create the model
        fileSystemModel = new FileSystemModel(new File(File.separator));
        // use the model for the Tree
        checkBoxTree = new CheckBoxTree(fileSystemModel);
        checkBoxTree.setEditable(false);
        // model for the checkboxes (not the directory structure)
        selectionModel = checkBoxTree.getCheckBoxTreeSelectionModel();
        // event listener
        checkBoxTree.getCheckBoxTreeSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {
            public void valueChanged(TreeSelectionEvent e) {
                System.out.println(selectionModel.getSelectionPath());
            }
        });

        // setup a little UI window for the tree.
        main_frame = new JFrame("Frame Title");
        main_frame.add(checkBoxTree);
        main_frame.setSize(400, 400);
        main_frame.setVisible(true);

        // run the loading test
        runTest();
    }

    public void runTest(){
        File[] finalPath = new File[3];
        finalPath[0] = (File)selectionModel.getModel().getRoot();
        finalPath[1] = new File(finalPath[0],"Documents and Settings");
        finalPath[2] = new File(finalPath[1],"Administrator");

        selectionModel.setSelectionPath(new TreePath(finalPath));
    }
}

谢谢!!

EN

回答 1

Stack Overflow用户

发布于 2011-11-04 23:36:12

CheckBoxTreeSelectionModel基本上是一个DefaultTreeSelectionModel (就像在Swing中一样)。树路径必须存在于TreeModel中的诀窍。我认为在runTest中创建TreePath的方式不会创建相同的树路径。最好从树上获取树的路径。试试这个,它会起作用的。

代码语言:javascript
复制
checkBoxTree.getCheckBoxTreeSelectionModel().addSelectionPath(checkBoxTree.getPathForRow(2));
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5968293

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档