1

Any one knows how to draw a Tree using a Path with Java swing (method paint(), image icon, ...)? I must draw a Tree from an existing Path.

This is my Tree (= "Arbre") class code for the recursive tree and the Test class, but I have to draw it.

class Arbre:

package org.mql.java.arbre;

import java.io.File;
import java.util.StringTokenizer;
import java.util.Vector;

public class Arbre {
    private String name;
    private Vector<Arbre> childs;
    int niveau = 0;

    public Arbre() {

    }

    public Arbre(String path, Vector<Arbre> childs) {
        super();
        this.name = path;
        this.childs = childs;
    }

    public String getPath() {
        return name;
    }

    public void setPath(String path) {
        this.name = path;
    }

    public Vector<Arbre> getChilds() {
        return childs;
    }

    public void setChilds(Vector<Arbre> childs) {
        this.childs = childs;
    }

    public String toString() {
        return "\nTree [name=" + name + ", childs=" + childs + "]";
    }

    public  void afficher(String path){
        afficherWrapper(path, path.split("\\\\").length);
    }

    public void afficherWrapper(String path , int skip) {
        File f = new File(path);
        if (f.isDirectory()) {
            int niveau = f.getAbsolutePath().split("\\\\").length;
            String output = "";
            for(int i=0 ; i < niveau-skip ; i++) output += "     " ;
            output += f.getName() + ">" ;
            System.out.println(output);
            File[] files = f.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isFile()) {
                    int niveauFile = files[i].getAbsolutePath().split("\\\\").length;
                    String outputF = "";
                    for(int j=0 ; j < niveauFile-skip ; j++) outputF += "     " ;
                    System.out.println(outputF+files[i].getName());
                }
                if (files[i].isDirectory()) {
                    this.afficherWrapper(files[i].getPath() ,skip);
                }
            }
        } else {
            System.out.println("veuillez entrez le chemin d'un répertoire");
        }
    }

    public void load(String path) {
        File f = new File(path);
        if (f.isDirectory()) {
            this.setPath(f.getAbsolutePath());
            this.setChilds(this.getChildren(f));
        }
    }

    public Vector<Arbre> getChildren(File f) {
        Vector<Arbre> ts = new Vector<>();
        if (f.isDirectory()) {
            File[] files = f.listFiles();
            for (int i = 0; i < files.length; i++) {
                ts.add(new Arbre(files[i].getAbsolutePath(), this.getChildren(files[i])));
            }
        }
        if (f.isFile()) {
        }

        return ts;
    }

    public int getChildrenNumber(Arbre t)
    {
        int total=0;
        for (int i = 0; i < t.getChilds().size(); i++) {
            total=total+this.getChildrenNumber(t.getChilds().get(i));
        }
        total=total+t.getChilds().size();

        return total;
    }

    /* public void tester(String path) {
        File f = new File(path);
        if (f.isDirectory()) {
            File[] files = f.listFiles();
            for (int i = 0; i < files.length; i++) {
                File file = new File(files[i].getAbsolutePath());
                System.out.println(file.getName());
                this.tester(files[i].getAbsolutePath());
            }

        }
    }
     */ 

    public String getTitle(String path)
    {
        int i=path.lastIndexOf("\\");
        String s=path.substring(i+1, path.length());
        return s;
    }
}

class Test:

package org.mql.java.arbre;

public class Test {

    public Test() {
        exp02();
    }
    private void exp02() {

        Arbre Ar = new Arbre();
        //Ar.load("E:\\eclipsetmp\\eclipse\\tree");
        Ar.afficher("E:\\eclipsetmp\\eclipse\\tree");
    }
    public void exp01(){
        Arbre Ar = new Arbre();
        Ar.load("E:\\eclipsetmp\\eclipse\\tree");
        System.out.println(Ar);
    }

    public static void main(String[] args) {
        new Test();

    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
  • 3
    [How to Use Trees](http://docs.oracle.com/javase/tutorial/uiswing/components/tree.html)? – MadProgrammer Jan 12 '16 at 02:04
  • See also the [File Browser GUI](http://codereview.stackexchange.com/q/4446/7784). – Andrew Thompson Jan 12 '16 at 05:07
  • im not asking for the JTree ,i want to draw the tree from any Path given like this exemple ![Valid XHTML](https://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-xfa1/v/t35.0-12/12494579_10208421259319005_431957248_o.jpg?oh=90d1702d386b887c1a4502f97255ab8c&oe=5697FDD9&__gda__=1452737297_ebf9f3665a8e99ec9a54990df035d97b). – nasser eddine Nadir Jan 12 '16 at 18:11
  • Where are you stuck? DO you have the file hierarchy? Do you have an algorithm in mind? Did you paint anything? – user1803551 Jan 12 '16 at 22:21
  • that's the problem i have no idea in mind for the algorithm , and no i didn't paint anything (that the probleme i don't know how to paint it ,it's my first project with swing i have never work with it ) : ( – nasser eddine Nadir Jan 13 '16 at 01:16
  • Did you get the file hierarchy correctly? Also, use @ to reply to a user in comments. – user1803551 Jan 13 '16 at 03:13
  • @user1803551 yes i get it correct . – nasser eddine Nadir Jan 16 '16 at 02:30
  • Look [here](http://stackoverflow.com/questions/10126695/how-to-draw-a-tree-representing-a-graph-of-connected-nodes) and [here](http://stackoverflow.com/questions/7108287/tree-visualisation-with-java). – user1803551 Jan 16 '16 at 15:42

0 Answers0