Bonjour !
Bon je vous explique mon problème, je dois réaliser un explorateur de fichier assez basique, en utilisant nottament JTree
Ca marche a peu pres, sauf quand j'essaye d'ouvrir mes dossiers "Documents and Settings" et "Volume System Information"
Voilà l'erreur :
Et voilà mon modele (FileSystemModel qui implemente TreeModel)
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 Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at misc.tree.FileSystemModel.getChildCount(FileSystemModel.java:19) at javax.swing.tree.VariableHeightLayoutCache$TreeStateNode.expand(Unknown Source) at javax.swing.tree.VariableHeightLayoutCache$TreeStateNode.expand(Unknown Source) at javax.swing.tree.VariableHeightLayoutCache.ensurePathIsExpanded(Unknown Source) at javax.swing.tree.VariableHeightLayoutCache.setExpandedState(Unknown Source) at javax.swing.plaf.basic.BasicTreeUI.updateExpandedDescendants(Unknown Source) at javax.swing.plaf.basic.BasicTreeUI$Handler.treeExpanded(Unknown Source) at javax.swing.JTree.fireTreeExpanded(Unknown Source) at javax.swing.JTree.setExpandedState(Unknown Source) at javax.swing.JTree.expandPath(Unknown Source) at javax.swing.plaf.basic.BasicTreeUI.toggleExpandState(Unknown Source) at javax.swing.plaf.basic.BasicTreeUI.handleExpandControlClick(Unknown Source) at javax.swing.plaf.basic.BasicTreeUI.checkForClickInExpandControl(Unknown Source) at javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelection(Unknown Source) at javax.swing.plaf.basic.BasicTreeUI$Handler.mousePressed(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)
Bon d'après l'erreur, le probleme vient de getchildCount(), sauf qu'y'a aucune raison pour que la méthode fonctionne pour les autres fichier ...
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 package misc.tree; import javax.swing.tree.*; import java.io.*; import java.util.*; import javax.swing.event.*; public class FileSystemModel implements TreeModel { public File root; public ArrayList<TreeModelListener> listeTML = new ArrayList<TreeModelListener>(); public FileSystemModel(File root) { this.root = root; } public int getChildCount(Object parent) { File tmp = (File)parent; if (tmp.isDirectory() ) { return tmp.listFiles().length; } return 0; } public Object getChild(Object parent, int index) { File tmp = (File)parent; try { return new MyFile(tmp.listFiles()[index].getCanonicalPath()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public Object getRoot() { return root; } public int getIndexOfChild(Object parent, Object child) { for ( int i = 0 ; i < getChildCount(parent) ; i++) { if ( getChild(parent,i).equals(child) ) { return i; } } return -1; } public boolean isLeaf(Object node) { File tmp = (File)node; if ( tmp.isFile() ) { return true; } else return false; } public void addTreeModelListener(TreeModelListener l) { listeTML.add(l); } public void removeTreeModelListener(TreeModelListener l) { listeTML.remove(listeTML.indexOf(l)); } public void valueForPathChanged(TreePath path, Object newValue) { //fireTreeNodesChanged() } public void fireTreeNodesChanged(TreePath parentPath, int[] indices, Object[] children) { TreeModelEvent e = new TreeModelEvent((Object)this, parentPath, indices, children); for (int i = 0 ; i < listeTML.size() ; i++ ) { listeTML.get(i).treeNodesChanged(e); } } }
J'pense plutot que ca vient de getChild()
Dans le TP il nous est demandé de créer une classe qui extends File, pour pouvoir surcharger la méthode toString() puisque celle ci affiche normalement l'adresse complete du fuchier et que je voudrai qu'elle n'affiche que le nom
Donc j'vous mets ma classe MyFile
A la base, j'avais fait un simple caste pour renvoyer l'enfant, comme ci dessous
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 package misc.tree; import java.io.File; import java.net.*; public class MyFile extends File { public MyFile(String pathname) { super(pathname); } public MyFile(File parent, String child) { super(parent,child); } public MyFile(String parent, String child) { super(parent,child); } public MyFile(URI uri) { super(uri); } public String toString() { return getName(); } }
Mais j'obtenais une erreur (D'ailleurs si quelqu'un peut m'expliquer pourquoi :s)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 public Object getChild(Object parent, int index) { MyFile tmp = (MyFile)parent; return tmp.listFiles()[index]; }
Donc en fait, ce qu'il me faudrait, c'est, je pense :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 Exception in thread "main" java.lang.ClassCastException: java.io.File cannot be cast to misc.tree.MyFile at misc.tree.FileSystemModel.getChild(FileSystemModel.java:25) at javax.swing.tree.VariableHeightLayoutCache$TreeStateNode.expand(Unknown Source) at javax.swing.tree.VariableHeightLayoutCache$TreeStateNode.expand(Unknown Source) at javax.swing.tree.VariableHeightLayoutCache.rebuild(Unknown Source) at javax.swing.tree.VariableHeightLayoutCache.setModel(Unknown Source) at javax.swing.plaf.basic.BasicTreeUI.setModel(Unknown Source) at javax.swing.plaf.basic.BasicTreeUI$Handler.propertyChange(Unknown Source) at java.beans.PropertyChangeSupport.firePropertyChange(Unknown Source) at java.beans.PropertyChangeSupport.firePropertyChange(Unknown Source) at java.awt.Component.firePropertyChange(Unknown Source) at javax.swing.JTree.setModel(Unknown Source) at javax.swing.JTree.<init>(Unknown Source) at misc.tree.MainTree.main(MainTree.java:10)
Soit arriver a caster proprement l'Object "parent" en MyFile, soit arriver a construire un File (ou MyFile mais les constructeurs sont identiques) correctement, puisque le probleme doit venir d'ici : new MyFile(tmp.listFiles()[index].getCanonicalPath())
Voilà, merci d'avance :/
Partager