Bonjour à tous,
Comme l'indique le titre, j'aimerais pouvoir lire un fichier pdf dans une arborescence créée à partir d'un JTree.
Voici le code que j'ai pour ne parcourir que l'ensemble des dossiers et fichiers du lecteur C:\: Evidemment je suis sous windows xp et j'utilise Eclipse.
Est-ce que c'est possible? Je vous remercie d'avance
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 package gui; import java.io.File; import javax.swing.*; import javax.swing.event.TreeSelectionEvent; import javax.swing.event.TreeSelectionListener; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreeSelectionModel; public class Tree extends JFrame implements TreeSelectionListener { private JTree arbre; private DefaultMutableTreeNode racine; public Tree(){ this.setSize(300, 300); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setTitle("Les arbres"); //On invoque la méthode de construction de notre arbre listRoot(); this.setVisible(true); } private void listRoot(){ this.racine = new DefaultMutableTreeNode(); int count = 0; String s="C:\\"; for(File file : File.listRoots()){ String f= file.getAbsolutePath(); DefaultMutableTreeNode lecteur = new DefaultMutableTreeNode(f); System.out.println(f); if(f.equals(s)){ try { for(File nom : file.listFiles()){ if (nom.getName().equalsIgnoreCase("ASDCACHE")){ DefaultMutableTreeNode node = new DefaultMutableTreeNode(nom.getName()+"\\"); lecteur.add(this.listFile(nom, node)); } } } catch (NullPointerException e) {} this.racine.add(lecteur); } } //On crée, avec notre hiérarchie, un arbre arbre = new JTree(this.racine); arbre.setRootVisible(false); arbre.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); arbre.setToggleClickCount(1); arbre.addTreeSelectionListener((TreeSelectionListener) this); /*public void valueChanged(TreeSelectionEvent event) { if(arbre.getLastSelectedPathComponent() != null){ System.out.println(arbre.getLastSelectedPathComponent().toString()); } }*/ this.getContentPane().add(new JScrollPane(arbre)); } private DefaultMutableTreeNode listFile(File file, DefaultMutableTreeNode node){ int count = 0; if(file.isFile()) return new DefaultMutableTreeNode(file.getName()); else{ for(File nom : file.listFiles()){ DefaultMutableTreeNode subNode; if(nom.isDirectory()){ subNode = new DefaultMutableTreeNode(nom.getName()+"\\"); node.add(this.listFile(nom, subNode)); }else{ subNode = new DefaultMutableTreeNode(nom.getName()); } node.add(subNode); } return node; } } public void valueChanged(TreeSelectionEvent e) { if(arbre.getLastSelectedPathComponent() != null){ System.out.println(arbre.getLastSelectedPathComponent().toString()); } } }![]()
Partager