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 2 of 2

Thread: Variable updates after loop

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Variable updates after loop

    So I'm working on adding a progress bar to my app, and I have all the variables created to track the progress of a download, but when I try to edit the variables in a while loop they do not update until after the loop is finished. Below is a list of what happens.

    Download function begins
    I set the total bytes to the file size
    I set the current bytes to 0
    While loop begins
    I add 1 for each byte that is copied
    After 1 million bytes are copied I call the code below
    I print the current bytes and it displays the correct number
    I call the repaint function
    Inside the paint function I print the current bytes and it displays 0
    loop ends
    download function ends
    paint function is called later
    The print inside the paint function now shows the total current bytes were downloaded


    Does anyone know why the paint function is not reading the real value set in the loop until after the loop is over, and if so how do i fix this problem?


  2. #2
    Member
    Join Date
    Oct 2011
    Posts
    42
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Variable updates after loop

    Do you use any threads? If not, that is the problem, because the progress bar's GUI is freezing until the download tasks completes. You need to create two threads:
    - one for running the download task.
    - one for update the progress bar.

    java exception
    Last edited by hns1984; January 11th, 2012 at 06:58 PM.

Similar Threads

  1. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 08:57 PM
  2. [SOLVED] what is this? Variable?
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 7th, 2011, 11:32 PM
  3. Replies: 1
    Last Post: October 16th, 2010, 03:32 PM
  4. How to use the same variable in various JPanel?
    By danielpereira in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 19th, 2010, 12:08 PM
  5. [SOLVED] Trojans with last few updates of NetBeans
    By Melawe in forum Java IDEs
    Replies: 11
    Last Post: May 22nd, 2010, 08:33 AM