1

我有一棵有几个子树(类别)的树,我如何只在指定的子树中允许多选操作,例如:我可以多选类别中的节点,但如果我尝试选择其他类别中的节点,它将取消选择当前子树中的节点并选择一个新节点。

4

2 回答 2

4

JTree通过使用更改默认选择模型setSelectionModel

提供你自己的实现TreeSelectionModel

于 2013-02-05T15:59:21.250 回答
2

无需实现 TreeSelectionModel,使用默认模型实现 TreeSelectionListener 就足够了。这是一个半工作示例,需要更多调整才能完美工作,但它说明了原理:

    JFrame f = new JFrame("JTree test");
    f.setSize(300, 300);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTree t = new JTree();
    DefaultTreeModel model = (DefaultTreeModel) t.getModel();
    final TreeSelectionModel selectionModel = t.getSelectionModel();

    selectionModel.setSelectionMode(TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
    selectionModel.addTreeSelectionListener(new TreeSelectionListener() {
        @Override
        public void valueChanged(TreeSelectionEvent e) {
            // detect additive selections
            if (e.isAddedPath()) {
                TreePath selection = e.getPath();
                DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) selection
                        .getLastPathComponent();

                // a contrived way to detect selection of items within a category
                if (((String) selectedNode.getUserObject())
                        .startsWith("Item")) {
                    // get the new selection's category node
                    DefaultMutableTreeNode category = (DefaultMutableTreeNode) selection
                            .getParentPath().getLastPathComponent();

                    // deselect everything outside that category
                    ArrayList<TreePath> deselectList = new ArrayList<TreePath>();
                    for (TreePath path : selectionModel.getSelectionPaths()) {
                        if (!category.equals(path.getParentPath()
                                .getLastPathComponent())) {
                            deselectList.add(path);
                        }
                    }
                    selectionModel.removeSelectionPaths(deselectList
                            .toArray(new TreePath[deselectList.size()]));
                } else {
                    // completely prevent selection of categories
                    selectionModel.removeSelectionPath(selection);
                }
            }
        }
    });

    DefaultMutableTreeNode root = new DefaultMutableTreeNode();
    DefaultMutableTreeNode category1 = new DefaultMutableTreeNode("Category 1");
    DefaultMutableTreeNode category2 = new DefaultMutableTreeNode("Category 2");
    DefaultMutableTreeNode item1 = new DefaultMutableTreeNode("Item 1");
    DefaultMutableTreeNode item2 = new DefaultMutableTreeNode("Item 2");
    DefaultMutableTreeNode item3 = new DefaultMutableTreeNode("Item 3");
    DefaultMutableTreeNode item4 = new DefaultMutableTreeNode("Item 4");

    category1.add(item1);
    category1.add(item2);
    category2.add(item3);
    category2.add(item4);

    root.add(category1);
    root.add(category2);

    t.setRootVisible(false);
    model.setRoot(root);

    f.getContentPane().add(new JScrollPane(t));
    f.setVisible(true); 
于 2013-02-05T16:41:23.490 回答