J'ai trouvé un code sur internet pour utiliser le JTree. Ça fait 2 jours que j'essaye de trouver comment avoir le nom et le path (C:/Program Files/[...]) du fichier sélectionné. Quelqu'un à une idée?

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
public class Tree {
 
    protected static FileSystemView fsv = FileSystemView.getFileSystemView();
    private JPanel pnl = new JPanel(new BorderLayout());
    private File[] roots;
    private JTree tree;
 
    public Tree() {
        roots = File.listRoots();
        initialisation();
    }
 
    public Tree(String URL) {
        File directory = new File(URL);
        roots = directory.listFiles();
        initialisation();
    }
 
    private void initialisation() {
        final FileTreeNode rootTreeNode = new FileTreeNode(roots);
        tree = new JTree(rootTreeNode);
        tree.setCellRenderer(new FileTreeCellRenderer());
        tree.setRootVisible(false);
        tree.addTreeSelectionListener(new TreeSelectionListener() {
            @Override
            public void valueChanged(TreeSelectionEvent e) {
                // Comment écrire le path du fichier sélectionner dans le JTree en String?
            }
        });
    }
 
    private class FileTreeCellRenderer extends DefaultTreeCellRenderer {
 
        private Map<String, Icon> iconCache = new HashMap<String, Icon>();
        private Map<File, String> rootNameCache = new HashMap<File, String>();
 
        @Override
        public Component getTreeCellRendererComponent(JTree tree, Object value,
                boolean sel, boolean expanded, boolean leaf, int row,
                boolean hasFocus) {
            FileTreeNode ftn = (FileTreeNode) value;
            File file = ftn.file;
            String filename = "";
            if (file != null) {
                if (ftn.isFileSystemRoot) {
                    filename = this.rootNameCache.get(file);
                    if (filename == null) {
                        filename = fsv.getSystemDisplayName(file);
                        this.rootNameCache.put(file, filename);
                    }
                } else {
                    filename = file.getName();
                }
            }
            JLabel result = (JLabel) super.getTreeCellRendererComponent(tree,
                    filename, sel, expanded, leaf, row, hasFocus);
            if (file != null) {
                Icon icon = this.iconCache.get(filename);
                if (icon == null) {
                    icon = fsv.getSystemIcon(file);
                    this.iconCache.put(filename, icon);
                }
                result.setIcon(icon);
            }
            return result;
        }
    }
 
    private class FileTreeNode implements TreeNode {
 
        private File file;
        private File[] children;
        private TreeNode parent;
        private boolean isFileSystemRoot;
 
        public FileTreeNode(File file, boolean isFileSystemRoot, TreeNode parent) {
            this.file = file;
            this.isFileSystemRoot = isFileSystemRoot;
            this.parent = parent;
            this.children = this.file.listFiles();
            if (this.children == null) {
                this.children = new File[0];
            }
        }
 
        public FileTreeNode(File[] children) {
            this.file = null;
            this.parent = null;
            this.children = children;
        }
 
        @Override
        public Enumeration<?> children() {
            final int elementCount = this.children.length;
            return new Enumeration<File>() {
                int count = 0;
 
                @Override
                public boolean hasMoreElements() {
                    return this.count < elementCount;
                }
 
                @Override
                public File nextElement() {
                    if (this.count < elementCount) {
                        return FileTreeNode.this.children[this.count++];
                    }
                    throw new NoSuchElementException("Vector Enumeration");
                }
            };
 
        }
 
        @Override
        public boolean getAllowsChildren() {
            return true;
        }
 
        @Override
        public TreeNode getChildAt(int childIndex) {
            return new FileTreeNode(this.children[childIndex],
                    this.parent == null, this);
        }
 
        @Override
        public int getChildCount() {
            return this.children.length;
        }
 
        @Override
        public int getIndex(TreeNode node) {
            FileTreeNode ftn = (FileTreeNode) node;
            for (int i = 0; i < this.children.length; i++) {
                if (ftn.file.equals(this.children[i])) {
                    return i;
                }
            }
            return -1;
        }
 
        @Override
        public TreeNode getParent() {
            return this.parent;
        }
 
        @Override
        public boolean isLeaf() {
            return (this.getChildCount() == 0);
        }
    }
 
    public JPanel getPnl() {
        JScrollPane jsp = new JScrollPane(this.tree);
        jsp.setBorder(new EmptyBorder(0, 0, 0, 0));
        pnl.add(jsp);
        return pnl;
 
    }
 
    public JTree getJTree() {
        return tree;
    }
}