IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Composants Java Discussion :

Trouver le path de la sélection & son nom.


Sujet :

Composants Java

  1. #1
    Candidat au Club
    Profil pro
    Inscrit en
    Mars 2013
    Messages
    2
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mars 2013
    Messages : 2
    Points : 2
    Points
    2
    Par défaut Trouver le path de la sélection & son nom.
    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;
        }
    }

  2. #2
    Candidat au Club
    Profil pro
    Inscrit en
    Mars 2013
    Messages
    2
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mars 2013
    Messages : 2
    Points : 2
    Points
    2
    Par défaut
    J'ai réussi à régler mon problème en changeant mon code.

    Voici le nouveau code :
    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
     
    package Util;
     
    import java.awt.BorderLayout;
    import java.io.File;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTree;
    import javax.swing.border.EmptyBorder;
    import javax.swing.event.TreeSelectionEvent;
    import javax.swing.event.TreeSelectionListener;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.TreePath;
     
    /**
     *
     * @author killouds
     */
    public class Tree2 {
     
        private JPanel pnl = new JPanel(new BorderLayout());
        private DefaultMutableTreeNode racine;
        private String sélectionPath = null, sélectionNom;
        private JTree tree;
        private File[] roots;
     
        public Tree2() {
            roots = File.listRoots();
            initialisation();
        }
     
        public Tree2(String URL) {
            File directory = new File(URL);
            roots = directory.listFiles();
            initialisation();
        }
     
        private void initialisation() {
            racine = new DefaultMutableTreeNode();
            for (File file : roots) {
                DefaultMutableTreeNode lecteur = new DefaultMutableTreeNode(file.getAbsolutePath());
                try {
                    for (File nom : file.listFiles()) {
                        DefaultMutableTreeNode node = new DefaultMutableTreeNode(nom.getName() + "\\");
                        lecteur.add(listFile(nom, node));
                    }
                } catch (NullPointerException e) {
                }
                racine.add(lecteur);
            }
            tree = new JTree(racine);
            tree.setRootVisible(false);
            tree.addTreeSelectionListener(new TreeSelectionListener() {
                @Override
                public void valueChanged(TreeSelectionEvent e) {
                    if (tree.getLastSelectedPathComponent() != null) {
                        sélectionPath = getAbsolutePath(e.getPath());
                        sélectionNom = tree.getLastSelectedPathComponent().toString();
                    }
                }
            });
        }
     
        private DefaultMutableTreeNode listFile(File file, DefaultMutableTreeNode node) {
            int count = 0;
            if (file.isFile() && file.getName().contains("bmp")) {
                return new DefaultMutableTreeNode(file.getName());
            } else {
                System.out.println("1");
                File[] list = file.listFiles();
                if (list == null) {
                    return new DefaultMutableTreeNode(file.getName());
                }
                for (File nom : list) {
                    count++;
                    if (count < 5) {
                        DefaultMutableTreeNode subNode = null;
                        if (nom.isDirectory()) {
                            subNode = new DefaultMutableTreeNode(nom.getName() + "\\");
                            node.add(listFile(nom, subNode));
                        } else {
                            subNode = new DefaultMutableTreeNode(nom.getName());
                        }
                        node.add(subNode);
                    }
                }
                return node;
            }
        }
     
        private String getAbsolutePath(TreePath treePath) {
            String str = "";
            for (Object name : treePath.getPath()) {
                if (name.toString() != null) {
                    str += name.toString();
                }
            }
            return str;
        }
     
        public JScrollPane getScroll() {
            JScrollPane jsp = new JScrollPane(this.tree);
            jsp.setBorder(new EmptyBorder(0, 0, 0, 0));
            return jsp;
        }
     
        public JTree getTree() {
            return tree;
        }
     
        public String getSelectionPath() {
            return sélectionPath;
        }
     
        public String getSelectionNom() {
            return sélectionNom;
        }
    }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [XL-2007] comment trouver une colonne dans une BD par son nom via VBA
    Par chikitin dans le forum Macros et VBA Excel
    Réponses: 2
    Dernier message: 30/06/2010, 09h52
  2. Réponses: 2
    Dernier message: 25/03/2010, 12h21
  3. Réponses: 1
    Dernier message: 29/05/2008, 15h01
  4. [CRange] Comment trouver un CRange lorsque l'on connait son nom ?
    Par SmOkEiSBaD dans le forum Macros et VBA Excel
    Réponses: 5
    Dernier message: 29/05/2008, 15h00
  5. Réponses: 1
    Dernier message: 31/05/2006, 18h59

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo