Jtree select node

Résolu
cs_zemzemi Messages postés 11 Date d'inscription vendredi 31 décembre 2004 Statut Membre Dernière intervention 30 mars 2010 - 21 avril 2009 à 09:45
cs_zemzemi Messages postés 11 Date d'inscription vendredi 31 décembre 2004 Statut Membre Dernière intervention 30 mars 2010 - 21 avril 2009 à 17:49
bonjour
est-t-il possible de sélectionner un nœud dans un arbre juste on passant  par paramètre leur nom.
Par exemple : soit l'arbre suivant
root
   élément 1
        fils1.1
        fils1.2
        fils1.3
   élément 2
        fils2.1
        fils2.2
   élément 3
Si on passe par paramètre "fils1.2" (ou root, élément 1, fils1.2) donc ce dernier sélectionne directement.

merci d'avance
A voir également:

1 réponse

cs_zemzemi Messages postés 11 Date d'inscription vendredi 31 décembre 2004 Statut Membre Dernière intervention 30 mars 2010
21 avril 2009 à 17:49
// Search forward from first visible row looking for any visible node
// whose name starts with prefix.
int startRow = 0 ;
String prefix = "b";
TreePath path = tree.getNextMatch(prefix, startRow, Position.Bias.Forward);

// Search backward from last visible row looking for any visible node
// whose name starts with prefix.
startRow = tree.getRowCount()-1 ;
prefix = "b";
path = tree.getNextMatch(prefix, startRow, Position.Bias.Backward);

// Find the path (regardless of visibility) that matches the
// specified sequence of names
path = findByName(tree, new String[]{"JTree", "food", "bananas"} );

// Finds the path in tree as specified by the node array. The node array is a sequence
// of nodes where nodes[0] is the root and nodes[i] is a child of nodes[i-1].
// Comparison is done using Object.equals(). Returns null if not found.
public TreePath find(JTree tree, Object[] nodes) {
TreeNode root = (TreeNode)tree.getModel().getRoot();
return find2(tree, new TreePath(root), nodes, 0, false);
}

// Finds the path in tree as specified by the array of names. The names array is a
// sequence of names where names[0] is the root and names[i] is a child of names[i-1].
// Comparison is done using String.equals(). Returns null if not found.
public TreePath findByName(JTree tree, String[] names) {
TreeNode root = (TreeNode)tree.getModel().getRoot();
return find2(tree, new TreePath(root), names, 0, true);
}
private TreePath find2(JTree tree, TreePath parent, Object[] nodes, int depth, boolean byName) {
TreeNode node = (TreeNode)parent.getLastPathComponent();
Object o = node;

// If by name, convert node to a string
if (byName) {
o = o.toString();
}

// If equal, go down the branch
if (o.equals(nodes[depth])) {
// If at end, return match
if (depth == nodes.length-1) {
return parent;
}

// Traverse children
if (node.getChildCount() >= 0) {
for (Enumeration e=node.children(); e.hasMoreElements(); ) {
TreeNode n = (TreeNode)e.nextElement();
TreePath path = parent.pathByAddingChild(n);
TreePath result = find2(tree, path, nodes, depth+1, byName);
// Found a match
if (result != null) {
return result;
}
}
}
}

// No match at this branch
return null;
}
3
Rejoignez-nous