2012-06-29 13 views
7

Ho un problema con l'espansione dei nodi JTree. Inizio quando uso seleziona un nodo tutto tranne il percorso del nodo selezionato e il nodo selezionato stesso da comprimere. Ho provato conEspansione del percorso JTree specifico

tree.collapsePath(new TreePath(tree.getModel().getRoot())); 
tree.expandPath(new TreePath(e111.getPath())); 

qualcosa di simile, ma sembra avere alcun effetto e io davvero non hanno idea di come posso fare questo

Ecco come il mio albero appare:

http://img220.imageshack.us/img220/3450/jtreepng.png

Se clicco su 1.1.1 voglio che tutto il resto sia compresso ma il nodo 1.1.1 e gli elementi dal percorso ad esso.

Ecco l'applicazione che ho creato:

public class SelectableTree extends JFrame implements TreeSelectionListener { 
public static void main(String[] args) { 
    new SelectableTree(); 
} 

DefaultMutableTreeNode root; 
DefaultMutableTreeNode e1; 
DefaultMutableTreeNode e2; 
DefaultMutableTreeNode e3; 

DefaultMutableTreeNode e11; 
DefaultMutableTreeNode e22; 
DefaultMutableTreeNode e33; 

DefaultMutableTreeNode e111; 
DefaultMutableTreeNode e222; 
DefaultMutableTreeNode e333; 

DefaultMutableTreeNode aChild; 
private JTree tree; 
private JTextField currentSelectionField; 

public SelectableTree() { 
    super("JTree Selections"); 
    WindowUtilities.setNativeLookAndFeel(); 
    addWindowListener(new ExitListener()); 
    Container content = getContentPane(); 
    root = new DefaultMutableTreeNode("Root"); 

    e1 = new DefaultMutableTreeNode("1"); 
    e2 = new DefaultMutableTreeNode("2"); 
    e3 = new DefaultMutableTreeNode("3"); 

    e11 = new DefaultMutableTreeNode("1.1"); 
    e22 = new DefaultMutableTreeNode("2.2"); 
    e33 = new DefaultMutableTreeNode("3.3"); 

    e111 = new DefaultMutableTreeNode("1.1.1"); 
    e222 = new DefaultMutableTreeNode("2.2.2"); 
    e333 = new DefaultMutableTreeNode("3.3.3"); 

    root.add(e1); 
    root.add(e2); 
    root.add(e3); 
    e1.add(e11); 
    e2.add(e22); 
    e3.add(e33); 
    e11.add(e111); 
    e22.add(e222); 
    e33.add(e333); 

    tree = new JTree(root); 
    tree.addTreeSelectionListener(this); 
    content.add(new JScrollPane(tree), BorderLayout.CENTER); 
    currentSelectionField = new JTextField("Current Selection: NONE"); 
    content.add(currentSelectionField, BorderLayout.SOUTH); 
    setSize(250, 275); 
    setVisible(true); 
} 

public void valueChanged(TreeSelectionEvent event) { 
    tree.clearSelection(); 
    tree.collapsePath(new TreePath(tree.getModel().getRoot())); 
    tree.expandPath(new TreePath(e111.getPath())); 
} 

Edit: stranamente i tree.collapsePath funziona bene, sembra che tree.expandPath(new TreePath(e111.getPath())); è il problema

risposta

6

oggetti GUI swing devono essere costruiti e manipolati solo sul event dispatch thread.

enter image description here

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import javax.swing.JFrame; 
import javax.swing.JScrollPane; 
import javax.swing.JTextField; 
import javax.swing.JTree; 
import javax.swing.event.TreeSelectionEvent; 
import javax.swing.event.TreeSelectionListener; 
import javax.swing.tree.DefaultMutableTreeNode; 
import javax.swing.tree.TreePath; 

public class SelectableTree extends JFrame implements TreeSelectionListener { 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new SelectableTree(); 
      } 
     }); 
    } 
    DefaultMutableTreeNode root; 
    DefaultMutableTreeNode e1 = new DefaultMutableTreeNode("1"); 
    DefaultMutableTreeNode e2 = new DefaultMutableTreeNode("2"); 
    DefaultMutableTreeNode e3 = new DefaultMutableTreeNode("3"); 
    DefaultMutableTreeNode e11 = new DefaultMutableTreeNode("1.1"); 
    DefaultMutableTreeNode e22 = new DefaultMutableTreeNode("2.2"); 
    DefaultMutableTreeNode e33 = new DefaultMutableTreeNode("3.3"); 
    DefaultMutableTreeNode e111 = new DefaultMutableTreeNode("1.1.1"); 
    DefaultMutableTreeNode e222 = new DefaultMutableTreeNode("2.2.2"); 
    DefaultMutableTreeNode e333 = new DefaultMutableTreeNode("3.3.3"); 
    DefaultMutableTreeNode aChild; 
    private JTree tree; 
    private JTextField currentSelectionField; 

    public SelectableTree() { 
     super("JTree Selections"); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     root = new DefaultMutableTreeNode("Root"); 
     root.add(e1); 
     root.add(e2); 
     root.add(e3); 
     e1.add(e11); 
     e2.add(e22); 
     e3.add(e33); 
     e11.add(e111); 
     e22.add(e222); 
     e33.add(e333); 

     tree = new JTree(root); 
     tree.addTreeSelectionListener(this); 
     add(new JScrollPane(tree), BorderLayout.CENTER); 
     currentSelectionField = new JTextField("Current Selection: NONE"); 
     add(currentSelectionField, BorderLayout.SOUTH); 
     setSize(250, 275); 
     setLocationRelativeTo(null); 
     setVisible(true); 
    } 

    @Override 
    public void valueChanged(TreeSelectionEvent event) { 
     tree.expandPath(new TreePath(e11.getPath())); 
     currentSelectionField.setText(event.getPath().toString()); 
    } 
} 
3

Hai controllato la javadoc del metodo expandPath. Il metodo fa esattamente ciò che descrive:

Se l'ultimo elemento nel percorso è una foglia, ciò non avrà alcun effetto.

+0

Beh non ho notato che, ma anche allora se faccio tree.expandPath (nuova TreePath (E11 .getPath())); l'effetto è che vedo l'albero completamente espanso. Il che è strano perché lo stesso identico comando funziona quando lo metto nel costruttore. – Ivo

+0

@Robin ha ragione; il tuo esempio ha anche bisogno di più ['invokeLater()'] (http://download.oracle.com/javase/tutorial/uiswing/concurrency/initial.html), mostrato [qui] (http://stackoverflow.com/a/11260104/230513). – trashgod

3

Usa

tree.setSelectionPath(new TreePath(e111.getPath())); 

invece di

tree.expandPath(new TreePath(e111.getPath()));