1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
| //CTRL + SHIFT + O pour générer les imports nécessaires
public class Fenetre extends JFrame {
private JTree arbre;
private DefaultMutableTreeNode racine;
private DefaultTreeModel model;
private JButton bouton = new JButton("Ajouter");
public Fenetre(){
this.setSize(600, 700);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("JTree");
//On invoque la méthode de construction de l'arbre
initTree();
bouton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event) {
if(arbre.getLastSelectedPathComponent() != null){
JOptionPane jop = new JOptionPane();
String nodeName = jop.showInputDialog("Saisir un nom de noeud");
if(nodeName != null && !nodeName.trim().equals("")){
DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode)arbre.getLastSelectedPathComponent();
DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(nodeName);
parentNode.add(childNode);
model.insertNodeInto(childNode, parentNode, parentNode.getChildCount()-1);
model.nodeChanged(parentNode);
}
}
else{
System.out.println("Aucune sélection !");
}
}
});
this.getContentPane().add(bouton, BorderLayout.SOUTH);
this.setVisible(true);
}
private void initTree(){
File dir = new File("D:\\Bureau");
this.racine = listFile(dir,new DefaultMutableTreeNode(dir));
// On crée, avec notre hiérarchie, un arbre
arbre = new JTree(this.racine);
// Que nous plaçons sur le ContentPane de notre JFrame à l'aide d'un
// scroll
this.getContentPane().add(new JScrollPane(arbre));
this.model = new DefaultTreeModel(this.racine);
arbre.setModel(model);
arbre.setRootVisible(false);
arbre.setEditable(true);
arbre.addTreeSelectionListener(new TreeSelectionListener(){
public void valueChanged(TreeSelectionEvent event) {
if(arbre.getLastSelectedPathComponent() != null){
//La méthode getPath retourne un objet TreePath
System.out.println(getAbsolutePath(event.getPath()));
}
}
private String getAbsolutePath(TreePath treePath){
String str = "";
//On balaie le contenu de l'objet TreePath
for(Object name : treePath.getPath()){
//Si l'objet a un nom, on l'ajoute au chemin
if(name.toString() != null)
str += name.toString();
}
return str;
}
});
arbre.getModel().addTreeModelListener(new TreeModelListener() {
public void treeNodesChanged(TreeModelEvent evt) {
System.out.println("Changement dans l'arbre");
Object[] listNoeuds = evt.getChildren();
int[] listIndices = evt.getChildIndices();
for (int i = 0; i < listNoeuds.length; i++) {
System.out.println("Index " + listIndices[i] + ", noeud déclencheur : " + listNoeuds[i]);
}
}
public void treeNodesInserted(TreeModelEvent event) {
System.out.println("Un noeud a été inséré !");
}
public void treeNodesRemoved(TreeModelEvent event) {
System.out.println("Un noeud a été retiré !");
}
public void treeStructureChanged(TreeModelEvent event) {
System.out.println("La structure d'un noeud a changé !");
}
});
}
private DefaultMutableTreeNode listFile(File file, DefaultMutableTreeNode node){
int count = 0;
if(file.isFile())
return new DefaultMutableTreeNode(file.getName());
else{
File[] list = file.listFiles();
if(list == null)
return new DefaultMutableTreeNode(file.getName());
for(File nom : list){
DefaultMutableTreeNode subNode;
if(nom.isDirectory()){
subNode = new DefaultMutableTreeNode(nom.getName()+"\\");
node.add(this.listFile(nom, subNode));
}else{
subNode = new DefaultMutableTreeNode(nom.getName());
}
node.add(subNode);
}
return node;
}
}
public static void main(String[] args){
//Nous allons créer des fenêtres avec des look and feel différents
//Cette instruction permet de récupérer tous les look and feel du système
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (InstantiationException e) {}
catch (ClassNotFoundException e) {}
catch (UnsupportedLookAndFeelException e) {}
catch (IllegalAccessException e) {}
Fenetre fen = new Fenetre();
}
} |
Partager