Bonjour a tous !
Je suis débutant en Java , et j'ai du m'y mettre pour le boulot. Je dois faire un Jfilechooser avec un filtre XML , j'ai donc repris un tuto de Developpez.com sur le sujet mais .... ca plante et je ne sais pas pourquoi.
Voici la classe de Filtre :
Et enfin l'utilisation de mon JFileChooser qui plante :
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 public class FileFilterExtend extends FileFilter{ /* * Description of files accepted by the filter */ private String description; /* * Extensions of files accepted by the filter */ private List<String> extensions; /** Creates a new instance of FileFilterExtend * from description * * @param description Description of files accepted by the filter * @throws NullPointerException */ public FileFilterExtend(String description) { if (description == null){ throw new NullPointerException("The description can't be null."); } this.description = description; this.extensions = new ArrayList<String>(); } /** * Implementation of Filefilter */ public boolean accept(File f) { if(f.isDirectory() || extensions.size()==0) { return true; } String nomFichier = f.getName().toLowerCase(); for(String extension : extensions){ if(nomFichier.endsWith(extension)){ return true; } } return false; } /** * Implementation of Filefilter */ public String getDescription() { StringBuffer buffer = new StringBuffer(description); buffer.append(" ("); for(String extension : extensions){ buffer.append(extension).append(" "); } return buffer.append(")").toString(); } /** * Set the Description * * @param description Description of files accepted by the filter * @throws NullPointerException If the description is null */ public void setDescription(String description){ if(description == null){ throw new NullPointerException("The description can't be null."); } this.description = description; } /** * Add Extension to the fileChooser * * @param extension Extensions of files accepted by the filter * @throws NullPointerException If the extension is null */ public void addExtension(String extension){ if(extension == null){ throw new NullPointerException("The extension can't be null."); } extensions.add(extension); } /** * Remove the selected extension * * @param extension Extensions of files accepted by the filter */ public void removeExtension(String extension){ extensions.remove(extension); } /** * Clear all the extensions */ public void clearExtensions(){ extensions.clear(); } /** * Get the list of extensions */ public List<String> getExtensions(){ return extensions; } }
Je pense qu'il doit y avoir une belle erreur de débutant dessous mais je voit pas
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 private void userSelectFiles(){ //creates the list of file ArrayList<String> alf = new ArrayList<String>(); alf=null; //creates the Filechooser int ret = myChoice.showOpenDialog(null); //myChoice.showOpenDialog(null); if(ret == JFileChooser.APPROVE_OPTION){ //One or more file are selected for (File fs : myChoice.getSelectedFiles()){ String path = fs.getPath(); alf.add(path); } } }
Edit : voici l'init :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 //init Filechooser FileSystemView vueSysteme = FileSystemView.getFileSystemView(); //recovery of Directories File defaut = vueSysteme.getDefaultDirectory(); File home = vueSysteme.getHomeDirectory(); // end recovery of Directories FileFilterExtend filtre = new FileFilterExtend("Xml Files");//configure extension Filter filtre.addExtension(".xml"); JFileChooser myChoice = new JFileChooser("."); myChoice.addChoosableFileFilter(filtre);//end configure extension Filter myChoice.setMultiSelectionEnabled(true) ;//enable MultiSelection myChoice.setCurrentDirectory(new File("/"));//display all disks myChoice.changeToParentDirectory();//end display all disks
Partager