Bonjour à tous,
Alors je pense que ce que je cherche à faire est assez clair, sachant qu'en plus j'ai trouvé une solution ici. Cependant, j'ai un problème, à savoir que lorsque je sélectionne un fichier qui ne se trouve pas dans le répertoire racine mais dans un sous-répertoire, le double clic sur le fichier fonctionne bien mais pas le bouton pour ouvrir.
Voici mon code en une petite classe self-compilable qui illustre mon besoin :
Ma question: est-ce que c'est un bug Java ou est-ce que le FileSystemView est mal utilisé ?
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
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 import java.io.File; import java.io.IOException; import javax.swing.Icon; import javax.swing.JFileChooser; import javax.swing.filechooser.FileSystemView; public class JFileChooserTest { private static JFileChooser fileChooser; private static final File rootDirectory = FileSystemView.getFileSystemView().getHomeDirectory(); public static void main(String[] args) throws IOException { fileChooser = MyFileSystemView.createFileChooser(rootDirectory); int result = fileChooser.showOpenDialog(null); File file = null; if (result == JFileChooser.APPROVE_OPTION) { file = fileChooser.getSelectedFile(); } System.out.println("ChosenFile: " + file); } protected static class MyFileSystemView extends FileSystemView { /** * Le FileSystemView par défaut */ private final FileSystemView system = FileSystemView.getFileSystemView(); /** * Le répertoire de base */ private final File root; /** * Constructeur privé */ private MyFileSystemView(File root) { // On récupère le chemin canonique du répertoire this.root = root; } /** * Factory publique */ public static JFileChooser createFileChooser(File root) throws IOException { root = root.getCanonicalFile(); // On récupère le chemin canonique if (!root.isDirectory()) { throw new IllegalArgumentException("root must be a directory"); } return new JFileChooser(root, new MyFileSystemView(root)); } @Override public boolean isRoot(File f) { return this.root.equals(f.getAbsoluteFile()); } @Override public boolean isFileSystemRoot(File dir) { return this.root.equals(dir.getAbsoluteFile()); } @Override public File[] getRoots() { return new File[]{this.root.getAbsoluteFile()}; } @Override public File getHomeDirectory() { return this.root.getAbsoluteFile(); } @Override public File getDefaultDirectory() { return this.root.getAbsoluteFile(); } @Override public File getParentDirectory(File dir) { if (isRoot(dir)) { // Pas de parent director pour le répertoire root : return this.root; } return system.getParentDirectory(dir); } @Override public File createFileObject(String path) { // Fichier crée lorsqu'on tape le nom directement : // On crée le fichier selon le répertoire root : File file = new File(this.root, path); if (file.exists()) { return file; } // S'il n'existe pas on renvoi root return this.root; } @Override protected File createFileSystemRoot(File f) { return null; } // Pour tous les autres on se contente d'appeler le FileSystemView par défaut : @Override public File createFileObject(File dir, String filename) { return system.createFileObject(dir, filename); } @Override public File createNewFolder(File containingDir) throws IOException { return system.createNewFolder(containingDir); } @Override public File getChild(File parent, String fileName) { return system.getChild(parent, fileName); } @Override public File[] getFiles(File dir, boolean useFileHiding) { return system.getFiles(dir, useFileHiding); } @Override public String getSystemDisplayName(File f) { return system.getSystemDisplayName(f); } @Override public Icon getSystemIcon(File f) { return system.getSystemIcon(f); } @Override public String getSystemTypeDescription(File f) { return system.getSystemTypeDescription(f); } @Override public boolean isComputerNode(File dir) { return system.isComputerNode(dir); } @Override public boolean isDrive(File dir) { return system.isDrive(dir); } @Override public boolean isFileSystem(File f) { return system.isFileSystem(f); } @Override public boolean isFloppyDrive(File dir) { return system.isFloppyDrive(dir); } @Override public boolean isHiddenFile(File f) { return system.isHiddenFile(f); } @Override public boolean isParent(File folder, File file) { return system.isParent(folder, file); } @Override public Boolean isTraversable(File f) { return system.isTraversable(f); } } }
Si quelqu'un a une solution je suis preneur !
En vous remerciant d'avance pour votre contribution !
Partager