2011-09-07 21 views
5

Ho un Jtree e 2 pulsanti per selezionare e deselezionare tutti i nodi. Ho fatto un tentativo simili:JTree: selezione programmata di tutti i nodi

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

Il pulsante deselezionare sembra funzionare, ma il selezionare tutti espande soltanto la JTree e seleziona l'ultimo nodo. Penso che ogni volta che un nodo viene selezionato in modo programmatico, sto deselezionando il precedente.

JTree è configurato in questo modo:

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

risposta

6

l'deselezionare accade perché si sta impostando un nuovo percorso di selezione, invece di aggiungere. Nel ciclo dopo l'espansione, invece che fare

curvesTree.addSelectionPath(...) 

EDIT

lettura api è sempre istruttivo, anche dopo anni ;-) Appena trovato un metodo molto simper, che lascia tutto il lavoro per l'albero:

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

eccellente cattura, come hai fatto ... +1 – mKorbel

+0

Grazie, ho cercato per questo metodo di metà mattina :) –

0

sì è che possibile, per esempio:

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

codice casuale frammento di nuovo ;-) – kleopatra

0

Vorrei aggiungere alla risposta di kleopatra (basata sui miei dolori in crescita).

Nel mio particolare problema, avevo bisogno di aggiungere una voce di menu "Seleziona tutti i bambini" al menu di scelta rapida del nodo JTree. Pertanto, questa soluzione si applica a tutti i bambini di un nodo selezionato.

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

Il determineTreePaths(selectedNode, paths) chiamata ricorsiva è necessario per attraversare l'albero dal nodo selezionato tutta la strada fino ai nodi foglia. Questa soluzione funziona indipendentemente dalla profondità (per quanto a mia conoscenza). Quello che non posso dire è che è la soluzione più efficiente. Chiunque abbia una soluzione migliore, si senta libero di pubblicare una soluzione diversa o di modificarla.

L'implementazione del metodo è la seguente:

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