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

Java JTree:以编程方式选择所有节点,java,swing,select,jtree,unselect,Java,Swing,Select,Jtree,Unselect,我有一个Jtree和两个按钮来选择和取消选择所有节点。我做了这样的尝试: selectAll = new JButton("Select all"); selectAll.addActionListener(new ActionListener (){ @Override public void actionPerformed(ActionEvent e) { int row = 0; while (

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

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();
            }
        });
curvesTree = new JTree(rootNode);
curvesTree.setExpandsSelectedPaths(true);
curvesTree.getSelectionModel().setSelectionMode(TreeSelectionModel.
                  DISCONTIGUOUS_TREE_SELECTION);

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

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() {
    }
}

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

 curvesTree.addSelectionPath(...)
编辑

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

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

我想补充一下克利奥帕特拉的答案(基于我自己成长的痛苦)

在我的特殊问题中,我需要在
JTree
节点弹出菜单中添加一个“选择所有子项”菜单项。因此,此解决方案适用于选定节点的所有子节点

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

出色的接球,你怎么+1谢谢,我已经找了半天了:)
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);
   }
}