Search code examples
javaswingjprogressbar

increase the progressbar percentage


I've the below piece of code, where there is a progress bar that has to progressed.

public void createFiles(String srcText, String destText, JTextArea outputTextArea, JProgressBar progressBar) {
        String zipFilePath = srcText;
        String destDirectory = destText;
        UnZip unzipper = new UnZip();
        File dir = new File(zipFilePath);
        File[] files = dir.listFiles();
        System.out.println(files.length);
        double pBarInt = (double) files.length / 100;
        int count = 1;
        System.out.println(count);
        if (null != files) {
            for (int fileIntList = 0; fileIntList < files.length; fileIntList++) {
                System.out.println("coun in vlocj " + count);
                String ss = files[fileIntList].toString();
                if (null != ss && ss.length() > 0) {
                    try {
                        if (files[fileIntList].isDirectory())
                            continue;
                        unzipper.unzip(zipFilePath + ss.substring(ss.lastIndexOf("\\") + 1, ss.length()), destDirectory,
                                outputTextArea);
                        if ((fileIntList + 1) % pBarInt == 0) {
                            progressBar.setValue(count);
                            progressBar.update(progressBar.getGraphics());
                            count += 1;
                        }

                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            }
        }
    }

Here the value of files.length is 25.

My question is, since there are 25 files 1% of them would be 2.5%, can i increase my progress bar for every 4 files processed as 10% or can i show 2.5% whenever a file has been processed.

If files.length is greater than 100, i'm able to do it, but unable to understand for files less than 100.

please let me know how can i get this done.

Thanks


Solution

  • You might just have to set the maximum correctly. E.g.:

    progressBar.setMaximum(files.length);
    

    To get only the files and not the directories in the first place do:

    File[] files = dir.listFiles(new FilenameFilter() {   
        @Override
        public boolean accept(File file, String name) {
            return !file.isDirectory();
        }
    });
    

    I would write the whole bit like this:

    File[] files = dir.listFiles(new FilenameFilter() {   
        @Override
        public boolean accept(File file, String name) {
            return !file.isDirectory();
        }
    });
    progressBar.setMaximum(files.length);
    for (int i = 0; i < files.length; i++) {
        File f = files[i];
        try {
           unzipper.unzip(f.getAbsolutePath(), destDirectory, outputTextArea);
           progressBar.setValue(i);
           progressBar.update(progressBar.getGraphics());
       } catch (Exception ex) {
          ex.printStackTrace();
       }
    }
    

    And you might want to dispatch it in a thread. See the comment on your question.