JTree:以编程方式选择所有节点

时间:2011-09-07 11:16:47

标签: java swing select jtree unselect

我有一个Jtree和2个按钮来选择和取消选择所有节点。我做了这样的尝试:

selectAll = new JButton("Select all");
selectAll.addActionListener(new ActionListener (){
        @Override
        public void actionPerformed(ActionEvent e) {
                int row = 0;
                while (row < curvesTree.getRowCount())
                {
                    curvesTree.expandRow(row);
                    row++;
                }
            int entradasTree = curvesTree.getRowCount();
            for(int i=0; i<entradasTree; i++){
                TreePath path = curvesTree.getPathForRow(i);
                curvesTree.setSelectionPath(path);
            }
        }
    });

        unselectAll = new JButton("Unselect all");
        unselectAll.addActionListener(new ActionListener (){
            @Override
            public void actionPerformed(ActionEvent e) {
                curvesTree.clearSelection();
            }
        });

unselect按钮似乎正在工作,但select all仅扩展JTree并选择最后一个节点。我认为每次以编程方式选择节点时,我都会取消选择前一个节点。

JTree的配置如下:

curvesTree = new JTree(rootNode);
curvesTree.setExpandsSelectedPaths(true);
curvesTree.getSelectionModel().setSelectionMode(TreeSelectionModel.
                  DISCONTIGUOUS_TREE_SELECTION);

3 个答案:

答案 0 :(得分:6)

取消选择是因为您正在设置新的选择路径而不是添加。在扩展后的循环中,改为

 curvesTree.addSelectionPath(...)

编辑

阅读api总是很有启发性,即使经过多年;-)刚刚找到了一个简单的方法,将所有的工作都留给了树:

tree.setSelectionInterval(0, tree.getRowCount());

答案 1 :(得分:0)

是的,这是可能的,例如:

import java.awt.Dimension;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;

public class TreeWithMultiDiscontiguousSelections {

    public static void main(String[] argv) {
        JTree tree = new JTree();
        tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
        int treeSelectedRows[] = {3, 1};
        tree.setSelectionRows(treeSelectedRows);
        TreeSelectionListener treeSelectionListener = new TreeSelectionListener() {

            @Override
            public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
                JTree treeSource = (JTree) treeSelectionEvent.getSource();
                System.out.println("Min: " + treeSource.getMinSelectionRow());
                System.out.println("Max: " + treeSource.getMaxSelectionRow());
                System.out.println("Lead: " + treeSource.getLeadSelectionRow());
                System.out.println("Row: " + treeSource.getSelectionRows()[0]);
            }
        };
        tree.addTreeSelectionListener(treeSelectionListener);
        JFrame frame = new JFrame("JTree With Multi-Discontiguous selection");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new JScrollPane(tree));
        frame.setPreferredSize(new Dimension(380, 320));
        frame.setLocation(150, 150);
        frame.pack();
        frame.setVisible(true);
    }

    private TreeWithMultiDiscontiguousSelections() {
    }
}

答案 2 :(得分:0)

我想补充一下kleopatra的答案(基于我自己不断增长的痛苦)。

在我的特定问题中,我需要在TreeNode selectedNode = tree.getSelectionPath().getLastPathComponent(); // Expand tree from selected node... List<TreePath> paths = new ArrayList<TreePath>(); determineTreePaths(selectedNode, paths); // Recursive method call... TreePath[] treePaths = new TreePath[paths.size()]; Iterator<TreePath> iter = paths.iterator(); for (int i = 0; iter.hasNext(); ++i) { treePaths[i] = iter.next(); } if (paths.size() > 0) { TreePath firstElement = paths.get(0); setSelectionPath(firstElement); scrollPathToVisible(firstElement); } 节点弹出菜单中添加一个“Select All Children”菜单项。因此,此解决方案适用于所选节点的所有子节点。

determineTreePaths(selectedNode, paths)

需要private void determineTreePaths(TreeNode currentNode, List<TreePath> paths) { paths.add(new TreePath(((DefaultTreeModel) getDefaultTreeModel()).getPathToRoot(currentNode)); // Get all of my Children Enumeration<?> children = currentNode.children(); // iterate over my children while (children.hasMoreElements()) { TreeNode child = (TreeNode) children.nextElement(); determineTreePaths(child, paths); } } 递归调用以从所选节点一直遍历树到叶节点。无论深度如何,这个解决方案都能正常工作(据我所知)。我不能说的是它是最有效的解决方案。任何有更好解决方案的人,请随时发布不同的解决方案或编辑此解决方案。

方法实现如下:

{{1}}