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 :

créer une arborescence windows sous forme d'arbre java [FAQ]


Sujet :

Composants Java

  1. #1
    Futur Membre du Club
    Inscrit en
    Août 2002
    Messages
    12
    Détails du profil
    Informations forums :
    Inscription : Août 2002
    Messages : 12
    Points : 9
    Points
    9
    Par défaut créer une arborescence windows sous forme d'arbre java
    en fait, ce que je cherche à faire, c'est une représentation de l'arborescence de l'OS sous forme d'un arbre java.

    l'embettant est que je ne sais pas où récupérer les différents lecteurs présents, les hdd, ..........

    en fait, je ne sais pas où récupérer cette arborescence.

    si qqun a une solution, je l'en remercie

  2. #2
    Futur Membre du Club
    Inscrit en
    Août 2002
    Messages
    12
    Détails du profil
    Informations forums :
    Inscription : Août 2002
    Messages : 12
    Points : 9
    Points
    9
    Par défaut c bon, j'y suis arrivé.
    désolé de vous avoir dérangé mais je viens d'arriver à mes fins.

  3. #3
    Tao
    Tao est déconnecté
    Candidat au Club
    Inscrit en
    Août 2002
    Messages
    6
    Détails du profil
    Informations forums :
    Inscription : Août 2002
    Messages : 6
    Points : 2
    Points
    2
    Par défaut
    POST ta solution sa aidera peut être d'autres personnes qui s'oppose au même problème
    @+
    Tao =)

  4. #4
    Futur Membre du Club
    Inscrit en
    Août 2002
    Messages
    12
    Détails du profil
    Informations forums :
    Inscription : Août 2002
    Messages : 12
    Points : 9
    Points
    9
    Par défaut solution
    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
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
     
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;
     
    import javax.swing.*;
    import javax.swing.tree.*;
    import javax.swing.event.*;
     
    public class Arb extends JPanel //extends JFrame 
    {
      public static final ImageIcon ICON_COMPUTER = 
        new ImageIcon("pt2.JPG");
      public static final ImageIcon ICON_DISK = 
        new ImageIcon("pt4.JPG");
      public static final ImageIcon ICON_FOLDER = 
        new ImageIcon("bib.JPG");
      public static final ImageIcon ICON_EXPANDEDFOLDER = 
        new ImageIcon("bibouv.JPG");
     
      protected JTree  m_tree;
      protected DefaultTreeModel m_model;
      protected JTextField m_display;
     
      public Arb() {
      //  super("Arbre MICRO");
       // setSize(400, 300);
     
        DefaultMutableTreeNode top = new DefaultMutableTreeNode(
          new IconData(ICON_COMPUTER, null, "MICRO"));
     
        DefaultMutableTreeNode node;
          File[] roots = File.listRoots();
        for (int k=0; k<roots.length; k++) {
          node = new DefaultMutableTreeNode(
            new IconData(ICON_DISK, 
            null, new FileNode(roots[k])));
          top.add(node);
          node.add( new DefaultMutableTreeNode(
          new Boolean(true)));
        }
     
        m_model = new DefaultTreeModel(top);
        m_tree = new JTree(m_model);
        m_tree.getSelectionModel().setSelectionMode(
          TreeSelectionModel.SINGLE_TREE_SELECTION);
        m_tree.putClientProperty("JTree.lineStyle", "Angled");
        TreeCellRenderer renderer = new IconCellRenderer();
        m_tree.setCellRenderer(renderer);
        m_tree.addTreeExpansionListener(new DirExpansionListener());
        m_tree.addTreeSelectionListener(new DirSelectionListener());
        m_tree.setShowsRootHandles(true); 
        m_tree.setEditable(false);
     
     
        JScrollPane pane3 = new JScrollPane(m_tree);
        setLayout(new GridLayout(1,0));
        pane3.setBorder(BorderFactory.createTitledBorder("MICRO"));
        add(pane3);
     
    //    JScrollPane s = new JScrollPane();
    //    s.getViewport().add(m_tree);
    //    getContentPane().add(s, BorderLayout.CENTER);
    //
    //    m_display = new JTextField();
    //    m_display.setEditable(false);
    //    getContentPane().add(m_display, BorderLayout.NORTH);
     
    //    WindowListener wndCloser = new WindowAdapter() {
    //      public void windowClosing(WindowEvent e) {
    //        System.exit(0);
    //      }
    //    };
    //    addWindowListener(wndCloser);
    //        
    //    setVisible(true);
      }
     
      DefaultMutableTreeNode getTreeNode(TreePath path) {
        return (DefaultMutableTreeNode) (path.getLastPathComponent());
      }
     
      FileNode getFileNode(DefaultMutableTreeNode node) {
        if (node == null)
          return null;
        Object obj = node.getUserObject();
        if (obj instanceof IconData)
          obj = ((IconData)obj).getObject();
        if (obj instanceof FileNode)
          return (FileNode)obj;
        else
          return null;
      }
     
      // Make sure expansion is threaded and updating the tree model
      // only occurs within the event dispatching thread.
      class DirExpansionListener implements TreeExpansionListener
      {
        public void treeExpanded(TreeExpansionEvent event) {
          final DefaultMutableTreeNode node = getTreeNode(
            event.getPath());
          final FileNode fnode = getFileNode(node);
     
          Thread runner = new Thread() {
            public void run() {
              if (fnode != null && fnode.expand(node)) {
                Runnable runnable = new Runnable() {
                  public void run() {
                    m_model.reload(node);
                  }
                };
                SwingUtilities.invokeLater(runnable);
              }
            }
          };
          runner.start();
        }
     
        public void treeCollapsed(TreeExpansionEvent event) {}
      }
     
      class DirSelectionListener implements TreeSelectionListener
      {
        public void valueChanged(TreeSelectionEvent event) {
          DefaultMutableTreeNode node = getTreeNode(event.getPath());
            FileNode fnode = getFileNode(node);
            if (fnode != null)
              m_display.setText(fnode.getFile().getAbsolutePath());
            else
              m_display.setText("");
        }
      }
     
    //  public static void main(String argv[]) { 
    //  	try {
    //	     UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    //	} catch (Exception exc) {
    //	    System.err.println("Error loading L&F: " + exc);
    //	}
    //  	new Arb(); }
    }
     
    class IconCellRenderer extends JLabel implements TreeCellRenderer
    {
      protected Color m_textSelectionColor;
      protected Color m_textNonSelectionColor;
      protected Color m_bkSelectionColor;
      protected Color m_bkNonSelectionColor;
      protected Color m_borderSelectionColor;
     
      protected boolean m_selected;
     
      public IconCellRenderer() {
        super();
        m_textSelectionColor = UIManager.getColor(
          "Tree.selectionForeground");
        m_textNonSelectionColor = UIManager.getColor(
          "Tree.textForeground");
        m_bkSelectionColor = UIManager.getColor(
          "Tree.selectionBackground");
        m_bkNonSelectionColor = UIManager.getColor(
          "Tree.textBackground");
        m_borderSelectionColor = UIManager.getColor(
          "Tree.selectionBorderColor");
        setOpaque(false);
      }
     
      public Component getTreeCellRendererComponent(JTree tree, 
       Object value, boolean sel, boolean expanded, boolean leaf, 
       int row, boolean hasFocus)        
      {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
        Object obj = node.getUserObject();
        setText(obj.toString());
     
        if (obj instanceof Boolean)
          setText("Chargement.....");
     
        if (obj instanceof IconData) {
          IconData idata = (IconData)obj;
          if (expanded)
            setIcon(idata.getExpandedIcon());
          else
            setIcon(idata.getIcon());
        }
        else
          setIcon(null);
     
        setFont(tree.getFont());
        setForeground(sel ? m_textSelectionColor : 
          m_textNonSelectionColor);
        setBackground(sel ? m_bkSelectionColor : 
          m_bkNonSelectionColor);
        m_selected = sel;
        return this;
      }
     
      public void paintComponent(Graphics g) {
        Color bColor = getBackground();
        Icon icon = getIcon();
     
        g.setColor(bColor);
        int offset = 0;
        if(icon != null && getText() != null) 
          offset = (icon.getIconWidth() + getIconTextGap());
        g.fillRect(offset, 0, getWidth() - 1 - offset,
          getHeight() - 1);
     
        if (m_selected) {
          g.setColor(m_borderSelectionColor);
          g.drawRect(offset, 0, getWidth()-1-offset, getHeight()-1);
        }
        super.paintComponent(g);
      }
    }
     
    class IconData
    {
      protected Icon   m_icon;
      protected Icon   m_expandedIcon;
      protected Object m_data;
     
      public IconData(Icon icon, Object data) {
        m_icon = icon;
        m_expandedIcon = null;
        m_data = data;
      }
     
      public IconData(Icon icon, Icon expandedIcon, Object data) {
        m_icon = icon;
        m_expandedIcon = expandedIcon;
        m_data = data;
      }
     
      public Icon getIcon() { return m_icon; }
     
      public Icon getExpandedIcon() { 
        return m_expandedIcon!=null ? m_expandedIcon : m_icon;
      }
     
      public Object getObject() { return m_data; }
     
      public String toString() { return m_data.toString(); }
    }
     
    class FileNode
    {
      protected File m_file;
     
      public FileNode(File file) { m_file = file; }
     
      public File getFile() { return m_file; }
     
      public String toString() {
        return m_file.getName().length() > 0 ? m_file.getName() :
          m_file.getPath();
      }
     
      public boolean expand(DefaultMutableTreeNode parent) {
        DefaultMutableTreeNode flag =
          (DefaultMutableTreeNode)parent.getFirstChild();
        if (flag==null)      // No flag
          return false;
        Object obj = flag.getUserObject();
        if (!(obj instanceof Boolean))
          return false;      // Already expanded
     
        parent.removeAllChildren();  // Remove Flag
     
        File[] files = listFiles();
        if (files == null)
          return true;
     
        Vector v = new Vector();
     
        for (int k=0; k<files.length; k++) {
          File f = files[k];
          if (!(f.isDirectory()))
            continue;
     
          FileNode newNode = new FileNode(f);
     
          boolean isAdded = false;
          for (int i=0; i<v.size(); i++) {
            FileNode nd = (FileNode)v.elementAt(i);
            if (newNode.compareTo(nd) < 0) {
              v.insertElementAt(newNode, i);
              isAdded = true;
              break;
            }
          }
          if (!isAdded)
          v.addElement(newNode);
        }
     
        for (int i=0; i<v.size(); i++) {
          FileNode nd = (FileNode)v.elementAt(i);
          IconData idata = new IconData(Arb.ICON_FOLDER, 
            Arb.ICON_EXPANDEDFOLDER, nd);
          DefaultMutableTreeNode node = 
            new DefaultMutableTreeNode(idata);
          parent.add(node);
     
          if (nd.hasSubDirs())
            node.add(new DefaultMutableTreeNode( 
              new Boolean(true) ));
        }
        return true;
      }
     
      public boolean hasSubDirs() {
        File[] files = listFiles();
        if (files == null)
          return false;
        for (int k=0; k<files.length; k++) {
          if (files[k].isDirectory())
            return true;
        }
        return false;
      }
     
      public int compareTo(FileNode toCompare) { 
        return  m_file.getName().compareToIgnoreCase(
          toCompare.m_file.getName() ); 
      }
     
      protected File[] listFiles() {
        if (!m_file.isDirectory())
          return null;
        try {
          return m_file.listFiles();
        }
        catch (Exception ex) {
          JOptionPane.showMessageDialog(null, 
            "Error reading directory "+m_file.getAbsolutePath(),
            "Warning", JOptionPane.WARNING_MESSAGE);
              return null;
        }
      }
    }

    j'espère que ce code pourra aider qqun

Discussions similaires

  1. Récupérer le chemin d'une table sous forme d'arbre
    Par ForgetTheNorm dans le forum SQL Procédural
    Réponses: 9
    Dernier message: 25/07/2012, 16h09
  2. Formule mathématique sous forme d'arbre. [Java]
    Par Kun1812 dans le forum Langages de programmation
    Réponses: 1
    Dernier message: 24/04/2012, 11h50
  3. Réponses: 19
    Dernier message: 18/05/2010, 22h53
  4. implementer une faune sous forme d'arbre binaire.
    Par mansour67 dans le forum Algorithmes et structures de données
    Réponses: 3
    Dernier message: 01/04/2008, 17h22

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