Voici la classe qui implémenté TreeModel :
Dans une applet, je crée un Jtree avec pour Model la classe précédente, mais je n'arrive pas à supprimer ou ajouter un noeud. Si quelqu'un pouvait m'aider...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 package GestionDeMail; import java.util.*; import javax.swing.event.*; import javax.swing.tree.*; public class DossierTreeModel implements TreeModel { private List<Dossier> dossierList = new ArrayList<Dossier>(); public final String ROOT = "Racine"; private EventListenerList listenerList = new EventListenerList(); public DossierTreeModel(List<Dossier> liste) { dossierList.add(new Dossier(ROOT, -1, -2)); dossierList.addAll(liste); } public List<Dossier> getDossierList() { return this.dossierList; } public void addTreeModelListener(TreeModelListener arg0) { listenerList.add(TreeModelListener.class, arg0); } public void removeTreeModelListener(TreeModelListener arg0) { listenerList.remove(TreeModelListener.class, arg0); } public Object getChild(Object arg0, int arg1) { Dossier d = (Dossier) arg0; Dossier tab[] = new Dossier[this.getChildCount(d)]; int j = 0; for (int i = 0; i < this.dossierList.size(); i++) { if (this.dossierList.get(i).getIdPere() == d.getId()) { tab[j] = this.dossierList.get(i); j++; } } for (int k = 0; k < tab.length; k++) { if (k == arg1) { return tab[k]; } } return null; } public int getChildCount(Object arg0) { int nbChilds = 0; for (int i = 0; i < this.dossierList.size(); i++) { Dossier d = (Dossier) arg0; if (d.getId() == this.dossierList.get(i).getIdPere()) { nbChilds++; } } return nbChilds; } public int getIndexOfChild(Object arg0, Object arg1) { Dossier parent = (Dossier) arg0; Dossier child = (Dossier) arg1; for (int i = 0; i < getChildCount(parent); i++) { if (getChild(parent, i).equals(child)) return i; } return -1; } public Object getRoot() { return this.getDossierList().get(0); } public boolean isLeaf(Object arg0) { boolean result = false; if (this.getChildCount(arg0) == 0) { result = true; } return result; } public void ajouterDossier(Dossier d) { this.dossierList.add(d); } public void removeDossier(int id) { for (int i = 0; i < this.dossierList.size(); i++) { if (this.dossierList.get(i).getId() == id) { this.dossierList.remove(i); } } } public void valueForPathChanged(TreePath path, Object newValue) { } }
La suppression se fait après un clic sur un bouton mais le problème n'est pas la mais plutôt dans la modification du model de mon JTree
Partager