Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Could someone explain this to me?

  1. #1
    Member
    Join Date
    Aug 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Could someone explain this to me?

    I am working on making a GUI for a program that I had written a while back. I wanted to include a progress bar to show its progress. However, the progress bar does not refresh and show the current percentage, it merely stays blank until the program is finished at which time it shows 100%. How do I make it to where it shows real time progress?

    Here is my current code of the particular section of the GUI:

     private void beginSortButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                
            //Variables
            Scanner s = new Scanner(System.in);
            String warningMessage = "WARNING!: Organizing files can be somewhat dangerous." + "\n" + "Do you wish to continue? (y/n) : ";
            String userConfirmation;
            String currentDirectoryPath;
            String RootPath;
            String goAgain = "y";
            String goAgain2;
            Receipt[] sortedReceipts;
            ArrayList<Receipt> tempArrayList = new ArrayList<>();
            Vector filePaths = new Vector<String>();
            int value = 0;
     
            currentDirectoryPath = this.currentDirectory.getText();
            RootPath = this.rootPath.getText();
     
            Path p = Paths.get(currentDirectoryPath);
            DirectoryStream<Path> dir = null;
            try {
                dir = Files.newDirectoryStream(p);
            } catch (IOException ex) {
                Logger.getLogger(fileSortWindow.class.getName()).log(Level.SEVERE, null, ex);
            }
     
            Path newDir = Paths.get(RootPath);
            try {
                Files.createDirectories(newDir);
            } catch (IOException ex) {
                Logger.getLogger(fileSortWindow.class.getName()).log(Level.SEVERE, null, ex);
            }
     
            for (Path file : dir) {
                String name = file.getFileName().toString();
                if (name.endsWith(".receipt")) {
     
                    //String representation of the files directory.
                    String fileDir = currentDirectoryPath.toString() + "\\" + name;
                    filePaths.add(fileDir);
     
                    //Making new Receipt object and getting the info from the methods in order to sort into the directories.
                    Receipt newReceipt = null;
                    try {
                        newReceipt = new Receipt(fileDir);
                    } catch (FileNotFoundException ex) {
                        Logger.getLogger(fileSortWindow.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (IOException ex) {
                        Logger.getLogger(fileSortWindow.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    String firstName = newReceipt.getCustomerFirstName();
                    String lastName = newReceipt.getCustomerLastName();
                    String year = Integer.toString(newReceipt.getYear());
                    String month = Integer.toString(newReceipt.getMonth());
                    String wholeName = lastName + ", " + firstName;
     
                    //Progress bar.
                    progress.setValue(value);
                    progress.repaint();
     
     
                    //Creates the new path for the file.
                    Path tempDir = Paths.get(RootPath, wholeName, year, month);
                    try {
                        Files.createDirectories(tempDir);
                    } catch (IOException ex) {
                        Logger.getLogger(fileSortWindow.class.getName()).log(Level.SEVERE, null, ex);
                    }
     
                    //Adds 1 to the progress bar.
                    value++;
     
     
     
                    Path newPath = Paths.get(tempDir.toString(), name);
     
     
                    if (Files.notExists(newPath)) {
                        try {
                            Files.copy(file, newPath);
                        } catch (IOException ex) {
                            Logger.getLogger(fileSortWindow.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
            }
     
            this.output.setText("Done.");
     
        }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Could someone explain this to me?

    If the code is executing on the EDT, then nothing will be updated until the jvm gets control of the EDT. Read the API doc for the SwingWorker class.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Could someone explain this to me?

    Can you isolate the mechanism you're using to increase the progress bar from a minimum to the maximum and just show that part?

    I suspect what you're seeing is the task starting at the minimum (maybe) and then a full progress bar when complete but there's essentially no visible difference between the two states because they occur so close together. You may have to slow down the activity by using a timer or similar device to show the progress at a perceptible speed, but then do you really want to make the user wait for a frill that they wouldn't normally have to wait for?

  4. #4
    Member
    Join Date
    Aug 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Could someone explain this to me?

    Someone told me that I need to make the progress bar update in a separate thread but I'm having a hard time figuring out how to do that.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Could someone explain this to me?

    Does the task that the progress bar represents take long enough for incremental displays to be made?

    Did you read the API doc for the SwingWorker class?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Aug 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Could someone explain this to me?

    Yea I did. And when you run the program it takes a about 30 seconds or more to run through. It sorts 3000 files into multiple directories.

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Could someone explain this to me?

    Is the task that does the sorting of 3,000 files done on the EDT or in a separate thread? More importantly, does the task doing the sorting track its progress? Can you get from the sorting task its progress using a method, something like:

    sortTask.getProgress();

    If so, it would be easy to create a javax.swing.Timer task to retrieve that progress and set the progress bar accordingly.

Similar Threads

  1. Could someone explain this to me?
    By blobman23 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 18th, 2013, 08:36 PM
  2. Could someone please better explain this to me?
    By blobman23 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 25th, 2013, 02:51 PM
  3. [SOLVED] Can someone explain to me?
    By unleashed-my-freedom in forum Java Theory & Questions
    Replies: 5
    Last Post: July 3rd, 2012, 04:10 AM
  4. Replies: 1
    Last Post: December 13th, 2010, 05:13 AM
  5. can anyone explain this?
    By chronoz13 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 12th, 2009, 02:51 AM