Liaison entre deux classe
slt j'ai fait un programme qui affiche un jtree j'ai 2 classes main pricipale ou se trouve l'interface et autre classe pour l'affichage du tree mon probleme c'est que je veux quand je selectionne un noeud il affiche dans un textfield pour cela j'ai creer textfield dans main pricipale et son action dans 2 eme classe mais ça marche pas
1 réponse
-
Contributeur'lut
mais ça marche pas
Très descriptif... déjà, tu utilises quel toolkit d'interface, et quel est le code que tu as mis? (à mettre entre balises <code java> quand tu le copieras ici)-
slt j'utilises java swing j'ai creer textfield dans la classe "mainprincipale" :
< currentSelectionField = new JTextField("Current Selection: NONE");
panel.add(currentSelectionField );>
et pour 2 eme classe j'ai mis :
<public void valueChanged1(TreeSelectionEvent event) {
jtree.expandPath(new TreePath(a.getPath()));
currentSelectionField.setText(event.getPath().toString());>
} -
Contributeur@mouna310
jtree.expandPath(new TreePath(a.getPath()));
C'est quoi la variablea
? Et ta classeimplements TreeSelectionListener
? As-tu pensé àjtree.addTreeSelectionListener(<instance du listener>);
?
PS: les balises code s'utilisent comme ceci: <@code java> ICI TON CODE </code>, sans le @. -
@ElementWmerci bcq pour ta réponse la variable "a" c'est le noeud
public JTree buildTree( String base) { JTree jtree = null; ArrayList dnList = getDN(base); Iterator it = dnList.iterator(); DefaultMutableTreeNode root = new DefaultMutableTreeNode(base); int baseLenth = base.split(",").length + 1; System.out.println(baseLenth); while (it.hasNext()) { String dnStr = (String) it.next(); String[] nodes = dnStr.split(","); int j = nodes.length; int k = j - baseLenth; DefaultMutableTreeNode a = root; for (int i = k; i >= 0; i--) { String nodeDN = getCreatedDn(nodes, i, k); if (existNodes.containsKey(new String(nodeDN))) { a = (DefaultMutableTreeNode) existNodes.get(new String( nodeDN)); continue; } DefaultMutableTreeNode b = new DefaultMutableTreeNode(nodes[i]); existNodes.put(new String(nodeDN), b); a.add(b); a = b; } a = null; } existNodes = null; jtree = new JTree(root); return jtree; }, -
@mouna310et pour
implements TreeSelectionListener
je l'éssaye mais ça marche pas -
Contributeur@mouna310Relis ma réponse, la solution est dans une des questions.
-