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

  1. 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)
    0
    1. 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());>
      }
      0
    2. Contributeur
      @mouna310
      jtree.expandPath(new TreePath(a.getPath())); 

      C'est quoi la variable
      a
      ? Et ta classe
      implements 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 @.
      0
    3. @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;
            }
      ,
      0
    4. @mouna310et pour
       implements TreeSelectionListener 
      je l'éssaye mais ça marche pas
      0
    5. Contributeur
      @mouna310Relis ma réponse, la solution est dans une des questions.
      0