0

JIDE Common Layer パッケージ (http://jidesoft.com/products/oss.htm) の一部である CheckBoxTree クラスを使用しています。私ができるようにしたいのは、どのボックスがチェックされているかどうかを追跡する CheckBoxTreeSelectionModel の状態を保存してロードすることです。selectionModel.getSelectionPaths() を保存するだけで保存できますが、読み込みに問題があります。selectionModel.setSelectionPaths() を実行すると、パスのルートとリーフのボックスのみがチェックされ、その間には何もチェックされません。奇妙なことに、これは、getSelectionPaths() の結果を保存し、それを直接 setSelectionPaths() にフィードした場合にも発生します。

FileSystemModel については、TreeNode の代わりに File オブジェクトを使用するのが好きなコードを見つけました。ネット上のさまざまな場所で見つけた FileSystemModel と CheckBoxTree のさまざまな組み合わせを試しましたが、結果は常に同じです。私はおそらくこの問題に 20 時間近く費やしました... 認めるのは少し恥ずかしいことです。どんな助けでも大歓迎です!

私のコードは次のとおりです。これにより、CheckBoxTree が作成され、"/Documents and Settings/Administrator" を使用して読み込もうとします。その結果、"/" と "Administrator" とそのすべての子がチェックされますが、"Documents and Settings" はチェックされません。

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

ありがとう!!

4

1 に答える 1

2

CheckBoxTreeSelectionModelは、基本的にDefaultTreeSelectionModelです(Swingの場合と同様)。ツリーパスがTreeModelに存在しなければならないトリック。runTestでTreePathを作成する方法で、同じツリーパスが作成されるとは思いません。ツリーからツリーパスを取得することをお勧めします。これを試してみてください、それはうまくいくでしょう。

checkBoxTree.getCheckBoxTreeSelectionModel().addSelectionPath(checkBoxTree.getPathForRow(2));
于 2011-11-04T15:36:11.810 に答える