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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
public class Fenetre extends JFrame {
private JTree arbre;
private DefaultMutableTreeNode racine;
//Notre objet modèle
private DefaultTreeModel model;
private JButton bouton = new JButton("ajouter"), efface=new JButton("effacer"), annoter=new JButton("Annoter");
//On passe maintenant le nom du look and feel à utiliser en paramètre du constructeur
public Fenetre(){
this.setSize(400, 400);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("JTree");
listRoot();
this.setVisible(true);
bouton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
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 SELECTION ! ! !");
}
}
}
});
efface.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
if(arbre.getLastSelectedPathComponent()!= null){
DefaultMutableTreeNode NoeudSupp=(DefaultMutableTreeNode)arbre.getLastSelectedPathComponent();
DefaultMutableTreeNode parentNoeud=(DefaultMutableTreeNode)NoeudSupp.getParent();
model.removeNodeFromParent(NoeudSupp);
model.nodeChanged(parentNoeud);
}
else{
System.out.println("AUCUNE SELECTION ! ! !");
}
}
});
annoter.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if(arbre.getLastSelectedPathComponent()!= null){
// Construction d'un afficheur par défaut.
DefaultTreeCellRenderer myRenderer = new DefaultTreeCellRenderer();
// Changement de l'icône pour les feuilles de l'arbre.
myRenderer.setLeafIcon(new ImageIcon("attribute_giallo.png"));
// Changement de l'icône pour les noeuds fermés.
//myRenderer.setClosedIcon(new ImageIcon("closedBookIcon.png"));
// Changement de l'icône pour les noeuds ouverts.
//myRenderer.setOpenIcon(new ImageIcon("openBookIcon.png"));
// Application de l'afficheur à l'arbre.
arbre.setCellRenderer(myRenderer);
}
}
});
JPanel pan = new JPanel();
pan.add(bouton);
pan.add(efface);
pan.add(annoter);
this.getContentPane().add(pan,BorderLayout.SOUTH);
}
private void listRoot(){
this.racine = new DefaultMutableTreeNode();
int count = 0;
for(File file : File.listRoots())
{
DefaultMutableTreeNode lecteur = new DefaultMutableTreeNode(file.getAbsolutePath());
try {
for(File nom : file.listFiles()){
DefaultMutableTreeNode node = new DefaultMutableTreeNode(nom.getName()+"\\");
lecteur.add(this.listFile(nom, node));
}
} catch (NullPointerException e) {}
this.racine.add(lecteur);
}
//On crée notre modèle
this.model = new DefaultTreeModel(this.racine);
//Et nous allons écouter ce que notre modèle a à nous dire !
this.model.addTreeModelListener(new TreeModelListener() {
/**
* Méthode appelée lorsqu'un noeud a changé
*/
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] + ", nouvelle valeur : "
+ listNoeuds[i]);
}
}
/**
* Méthode appelée lorsqu'un noeud est inséré
*/
public void treeNodesInserted(TreeModelEvent event) {
System.out.println("Un noeud a été inséré !");
}
/**
* Méthode appelée lorsqu'un noeud est supprimé
*/
public void treeNodesRemoved(TreeModelEvent event) {
System.out.println("Un noeud a été retiré !");
}
/**
* Méthode appelée lorsque la structure d'un noeud a été modifiée
*/
public void treeStructureChanged(TreeModelEvent event) {
System.out.println("La structure d'un noeud a changé !");
}
});
//On crée, avec notre hiérarchie, un arbre
arbre = new JTree();
//On passe notre modèle à notre arbre
//===> on pouvait aussi passer l'objet TreeModel au constructeur du JTree
arbre.setModel(model);
arbre.setRootVisible(false);
//On rend notre arbre éditable
arbre.setEditable(true);
this.getContentPane().add(new JScrollPane(arbre), BorderLayout.CENTER);
}
private DefaultMutableTreeNode listFile(File file, DefaultMutableTreeNode node){
int count = 0;
if(file.isFile())
return new DefaultMutableTreeNode(file.getName());
else{
for(File nom : file.listFiles()){
count++;
//pas plus de 5 enfants par noeud
if(count < 3){
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 looks différents
//Cette instruction permet de récupérer tous les looks 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