0

hey guys i m devloping an application to read directories and subdirectories and files so this works perfectly

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package avd;

import java.io.File;



/**
 *
 * @author USER
 */
public class arborescence {
   private String initialpath = "";
    private Boolean recursivePath = false;
    public int filecount = 0;
    public int dircount = 0;

/**
 * Constructeur
 * @param path chemin du répertoire
 * @param subFolder analyse des sous dossiers
 */
    public arborescence(String path, Boolean subFolder) {
        super();
        this.initialpath = path;
        this.recursivePath = subFolder;
    }

    public void list() {
        this.listDirectory(this.initialpath);
    }

    private void listDirectory(String dir) {
        File file = new File(dir);
        File[] files = file.listFiles();
        if (files != null) {
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory() == true) {
                    System.out.println("Dossier: " + files[i].getAbsolutePath());
                    FirstWindow.lblRepertoire.setText(files[i].getAbsolutePath());
                    this.dircount++;
                } else {
                    System.out.println("  Fichier: " + files[i].getName());
                    FirstWindow.lblFile.setText(files[i].getAbsolutePath());
                   this.filecount++;
                }
                if (files[i].isDirectory() == true && this.recursivePath == true) {
                    this.listDirectory(files[i].getAbsolutePath());
                }
            }
        }
    }    
}

and in the main Class i ve this

public void  scanfiles(){
 String pathToExplore = fileName;
 arborescence arbo = new arborescence(pathToExplore, true);
  Long start = System.currentTimeMillis();
  arbo.list();
  System.out.println("----------");
  System.out.println("Analyse de " + pathToExplore + " en " + (System.currentTimeMillis() - start) + " mses");
  lbltime.setText(Integer.toString((int) (System.currentTimeMillis() - start)));
  System.out.println(arbo.dircount + " dossiers");
  lblCountRep.setText(Integer.toString(arbo.dircount));
  System.out.println(arbo.filecount + " fichiers");
  lblCountFiles.setText(Integer.toString(arbo.filecount));
}

This is my Frame enter image description here

The Problem that in the Label "Repertoire" it shows me that the last one i wanna that it show me every one instatly like when the antivirus scan your files you see what it's scanning can we do that ?

  • 2
    Generally, you can use HTML to apply formatting to the text, for [example](http://stackoverflow.com/questions/29550524/jlabel-with-multiple-lines-and-alignment-to-the-right/29551195#29551195) and [example](http://stackoverflow.com/questions/14737810/jlabel-show-longer-text-as-multiple-lines/14738193#14738193) – MadProgrammer Aug 03 '15 at 12:45
  • @MadProgrammer it depends on the [``Document`` type](http://docs.oracle.com/javase/7/docs/api/javax/swing/text/html/HTMLDocument.html), but yes, basically you're right – Binkan Salaryman Aug 03 '15 at 12:51
  • @BinkanSalaryman Nope, much simpler then that just `This
    is
    some
    text` - Been working since Java 1.3 :)
    – MadProgrammer Aug 03 '15 at 12:52
  • @MadProgrammer What's your point here? – Binkan Salaryman Aug 03 '15 at 12:55
  • @BinkanSalaryman For something as simple as a `JLabel`, you don't need a `HTMLDocument`, you just need marked up text – MadProgrammer Aug 03 '15 at 12:57
  • I would consider using a `JTable` instead, much more control over the layout, see [How to Use Tables](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html) for more details – MadProgrammer Aug 03 '15 at 12:58
  • I m not looking for making two lines what i m looking for it is if he find a Directory it will be show for example Repertoire :C:\User\Samer\Folder1 and After that it erased and show you the next one so the label will be like that C:\User\Samer\Folder2 you get it ??? – Mosael Guilbert Aug 03 '15 at 14:04

2 Answers2

2

Make sure that the listDirectory() method is run in a separate thread. label setText call should be moved to EDT (use SwingUtilities.invokeLatter).

the problem is that EDT is working for iterating files, then it has no chance to repaint labels.

// call this method in separate thread , not in EDT
private void listDirectory(String dir) {
        File file = new File(dir);
        File[] files = file.listFiles();
        if (files != null) {
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory() == true) {
                    System.out.println("Dossier: " + files[i].getAbsolutePath());
                    FirstWindow.lblRepertoire.setText(files[i].getAbsolutePath()); // call in invoke latter
                    this.dircount++;
                } else {
                    System.out.println("  Fichier: " + files[i].getName());
                    FirstWindow.lblFile.setText(files[i].getAbsolutePath());  // call in invoke latter
                   this.filecount++;
                }
                if (files[i].isDirectory() == true && this.recursivePath == true) {
                    this.listDirectory(files[i].getAbsolutePath());
                }
            }
        }
    }    

EDIT:

 scanButton.addActionListener(new ActionListener() {
 public void actionPerformed(ActionEvent e) {
 Thread scanThread = new Thread() {
  public void run() {
    scanFiles();
  }
   };
  scanThread .start();
  }
  });
hunter
  • 3,963
  • 1
  • 16
  • 19
  • i didn't get the answer what i must change this method i move it in an other classe ? this what u mean ? – Mosael Guilbert Aug 03 '15 at 13:42
  • 1
    No, i have added a sample code, then scanFiles method will be called in new thread. but when u update GUI component again you have to do it in EDT. go though some good tutorials, you will understand the concept – hunter Aug 03 '15 at 16:08
0

This is an example how to do it. Not sure if you are looking for the same. Actually html do stuff for you.

JLabel label = new JLabel("<html>First line and maybe second line</html>");

Make your jlabel stretchable by using proper layout. May be grid bag layout. It should do the trick.

Community
  • 1
  • 1
  • Linking to some one else answer is discouraged unless you are using it as bases for your own answer, this is better suited to a comment – MadProgrammer Aug 03 '15 at 12:56
  • @MadProgrammer well that make sense – Sandeep Mandori Aug 03 '15 at 13:03
  • Apart from been copied from the original answer, it doesn't "really" answer the question of breaking the content up over multiple lines – MadProgrammer Aug 03 '15 at 13:06
  • I m not looking for making two lines what i m looking for it is if he find a Directory it will be show for example Repertoire :C:\User\Samer\Folder1 and After that it erased and show you the next one so the label will be like that C:\User\Samer\Folder2 you get it ??? – Mosael Guilbert Aug 03 '15 at 13:44