JTree: select all nodes programmatically

I have Jtree and 2 buttons to select and deselect all nodes. I made this attempt:

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

The unselect button seems to work, but select all only extends JTree and selects the last node. I think every time a node is selected programmatically, I do not select the first one.

JTree is configured as follows:

curvesTree = new JTree(rootNode);
curvesTree.setExpandsSelectedPaths(true);
curvesTree.getSelectionModel().setSelectionMode(TreeSelectionModel.
                  DISCONTIGUOUS_TREE_SELECTION);
+5
source share
3 answers

unselect happens because you are setting a new selection path instead of adding. In the loop after decomposition, we execute instead

 curvesTree.addSelectionPath(...)

EDIT

reading api is always instructive, even after several years ;-) I just found a very simple method that leaves all the work with the tree:

tree.setSelectionInterval(0, tree.getRowCount());
+6
source

, , :

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

kleopatra ( ).

" " JTree node. node.

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

determineTreePaths(selectedNode, paths) node . ( ). , . , , .

:

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

All Articles