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:
Il problema però che nella funzione creaNodi non mi viene aggiunta nessuna cartella.Non capisco il perchè.Grazie mille.
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());
}
}