Visualizzazione dei risultati da 1 a 4 su 4
  1. #1
    Utente di HTML.it
    Registrato dal
    Aug 2009
    Messaggi
    213

    java-aggiunta cartelle JTree

    Sto realizzando per esercizio un sorgente dove inserendo in una text il nome di una cartella premendo un button dovrei aggiungere questa cartella al JTree.Questo è il codice:

    codice:
    import javax.swing.*;
    import javax.swing.JTree;
    import javax.swing.tree.DefaultMutableTreeNode;
    import java.awt.event.*;
    
    
    public class TreeDemo1 extends JFrame implements ActionListener{
        
        
        
        private JTree tree;
        JTextField c=new JTextField(15);
        JButton d=new JButton("Aggiungi");
        JLabel x=new JLabel();
        
        DefaultMutableTreeNode top =new DefaultMutableTreeNode("The Java Series");
        DefaultMutableTreeNode category = null;
        
    
    public TreeDemo1() {
        super.setSize(300,300);
        
        
        createNodes(top);
        
        tree = new JTree(top);
        
        JScrollPane treeView = new JScrollPane(tree);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel pannello=new JPanel();
            tree.setBounds(12, 12, 373, 347);
            pannello.add(tree);
            pannello.add(c);
            pannello.add(d);
            pannello.add(x);
            setContentPane(pannello);
            d.addActionListener(this);
                
        }
    
    private void createNodes(DefaultMutableTreeNode top) {
        
        DefaultMutableTreeNode book = null;
        
         
        category = new DefaultMutableTreeNode("Books for Java Programmers");
        top.add(category);
        
        //original Tutorial
        //book = new DefaultMutableTreeNode("The Java Tutorial: A Short Course on the Basics");
        //category.add(book);
        
        //Tutorial Continued
        book = new DefaultMutableTreeNode("The Java Tutorial Continued: The Rest of the JDK");
        category.add(book);
        
        //Swing Tutorial
        book = new DefaultMutableTreeNode("The Swing Tutorial: A Guide to Constructing GUIs");
        category.add(book);
    
        //...add more books for programmers...
    
        category = new DefaultMutableTreeNode("Books for Java Implementers");
        top.add(category);
    
        //VM
        book = new DefaultMutableTreeNode("The Java Virtual Machine Specification");
        category.add(book);
    
        //Language Spec
        book = new DefaultMutableTreeNode("The Java Language Specification");
        category.add(book);
                         
        
    }
    private void creaNodi() {
       
       DefaultMutableTreeNode category = null;
          category=new DefaultMutableTreeNode(c.getText());
            top.add(category);
            x.setText(category.toString());
        }
    
    public static void main(String[] args){
        TreeDemo1 a=new TreeDemo1();
        a.setVisible(true);
    }
    
    public void actionPerformed(ActionEvent e) {
        creaNodi();
        //x.setText(c.getText());
    }
    }
    Il problema però che nella funzione creaNodi non mi viene aggiunta nessuna cartella.Non capisco il perchè.Grazie mille.

  2. #2
    Utente di HTML.it L'avatar di progAnd
    Registrato dal
    Jan 2012
    Messaggi
    119
    Stai modificando solo il modello e non la vista. Devi usare il metodo insertNodeInto:
    Se devi, ad esempio, aggiungere il nodo alla fine:
    codice:
    DefaultMutableTreeNode selectedNode=(DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
    if (selectedNode == null) return;
    DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(c.getText());
    top.insertModelInto(newNode, selectedNode, selectedNode.getChildCount());
    //ora si fa vedere all'utente il nuovo nodo
    TreeNode[] nodes=top.getPathToRoot(newNode);
    TreePath path=new TreePath(nodes);
    tree.scrollPathToVisible(path);
    Nota: top dovrebbe essere un DefaultTreeModel e non un DefaultMutableTreeNode.

    Ciao

  3. #3
    Utente di HTML.it
    Registrato dal
    Aug 2009
    Messaggi
    213
    Non mi riconosce il metodo insertIntoNode.Ma lo devo sviluppare?

  4. #4
    Utente di HTML.it L'avatar di progAnd
    Registrato dal
    Jan 2012
    Messaggi
    119
    top deve essere un DefaultTreeModel e non un DefaultMutableTreeNode.

    Ciao

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.