Results 1 to 5 of 5
  1. #1
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Progress bar question

    Hi,

    I have added a progress bar to my program.
    But I am not sure that it behave like I want...

    Java Code:
    JProgressBar progress = new javax.swing.JProgressBar(0,100);
    
    int count;
    int size = 20000000;
    for(count = 0; count <= size;count++) {
         
    int percent = (int)((count * 100.0f) / size);
            
    progress.setValue(percent);
            
    }
    Looks like it only show 0 and 100. Any comments?

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default Re: Progress bar question

    Have a look at the ProgressBar tutorial. Your code is all single threaded, it won't work that way.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,935
    Rep Power
    4

    Default Re: Progress bar question

    Actually it will work single threaded (not counting EDT). It works on my laptop. But then my laptop is very old so it may just be displaying too fast to see the progress on the OP's machine.

    Also, I checked the tutorials myself and found what I consider an egregious error on their part.
    From Java Tutorials Sample Code

    Java Code:
    /**  
      * Invoked when task's progress property changes.
      */
    public void propertyChange(PropertyChangeEvent evt) {
       if ("progress" == evt.getPropertyName()) {
           int progress = (Integer) evt.getNewValue();
           progressBar.setValue(progress);
           taskOutput.append(String.format(
             "Completed %d%% of task.\n", task.getProgress()));
       } 
    }
    Using == to check the property name. Perhaps they were taking advantage of interned strings but I think it should be changed.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,083
    Rep Power
    4

    Default Re: Progress bar question

    Quote Originally Posted by jim829 View Post
    But then my laptop is very old so it may just be displaying too fast to see the progress on the OP's machine.
    I don't know about that. Doesn't the EDT operate on an update interval which should be the same on both machines? I can't imagine your laptop is SO slow that it can't make the tick rate.

    Also, I checked the tutorials myself and found what I consider an egregious error on their part.
    Shocking and shameful indeed!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,935
    Rep Power
    4

    Default Re: Progress bar question

    Quote Originally Posted by gimbal2 View Post
    I don't know about that. Doesn't the EDT operate on an update interval which should be the same on both machines? I can't imagine your laptop is SO slow that it can't make the tick rate.
    It was a guess (perhaps a poor one on my part) since the OP isn't really doing anything wrong (I assume it was placed in a JPanel, etc, etc). It takes about 10 seconds for the entire progress bar to complete on my laptop.

    Correction! 2 seconds. I forgot I added a percent string to paint over the bar. That added 8 more seconds to the time to display.
    Edit: And I didn't even have to do that. It is done for you. Clearly I don't use progress bars much! :)

    Regards,
    Jim
    Last edited by jim829; 10-22-2013 at 04:00 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Progress Bar showing no progress
    By igorland in forum New To Java
    Replies: 8
    Last Post: 05-03-2013, 05:39 PM
  2. Progress Bar using Awt help
    By ynglsuresh in forum AWT / Swing
    Replies: 3
    Last Post: 03-28-2012, 07:33 AM
  3. Replies: 0
    Last Post: 03-01-2012, 12:21 PM
  4. Progress bar
    By Milanek in forum New To Java
    Replies: 0
    Last Post: 10-11-2011, 04:41 PM
  5. How to use Progress bar
    By LankanSniper in forum AWT / Swing
    Replies: 3
    Last Post: 03-25-2009, 10:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •