Results 1 to 12 of 12

Thread: jProgressBar - How???

  1. #1

    Thread Starter
    Hyperactive Member xxarmoxx's Avatar
    Join Date
    Mar 2007
    Posts
    378

    jProgressBar - How???

    This is my 4th attempt at trying to figure out how to use a progressbar, this is what I have but its not working:


    Code:
          class Counter extends Thread implements Runnable{
      /* This class simply runs a thread to count to 50 */
         public int count = 1;
    
          public void run() {
              while (count < 50) {
                  try {
                      sleep(1000);
                  Thread updateThread = new Thread(this, "Count Program");
                //updateThread.setPriority(NUMCOUNTERS+2);
                   jProgressBar1.setValue(count);
                  } catch (InterruptedException e) { }
                count++;
            }
    			stop();
        }    
     }
        
    }

    How can I simply make the progressbar move 1 second at a time.

    Help would be greatly appreciated

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: jProgressBar - How???

    Code:
        public void start(){
            Thread thread = new Thread(new Runnable() {
    
                private int count;
    
                public void run() {
                    while (count < 50) {
                        try {
                            Thread.sleep(1000);
                            //updateThread.setPriority(NUMCOUNTERS+2);
                            jProgressBar1.setValue(count);
                        } catch (InterruptedException e) {
                        }
                        count++;
                    }
                }
            }, "Count Program");
            thread.start();
        }
    I think you've forgotten to call the "thread.start()" method
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    Hyperactive Member xxarmoxx's Avatar
    Join Date
    Mar 2007
    Posts
    378

    Re: jProgressBar - How???

    So here is what I did:

    I put your void function within my class. As soon as the jButton is clicked I call start() and then a loop goes through a file and creates a model. The app still freezes while its looping through the file and I dont see the progressbar filling up. What do you think is wrong?

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: jProgressBar - How???

    Are you sure the "JButton" click event is handled?
    try a JOptionPane message to make sure its working

    Otherwise I have no idea what to do, because this should work for java 1.4
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5

    Thread Starter
    Hyperactive Member xxarmoxx's Avatar
    Join Date
    Mar 2007
    Posts
    378

    Re: jProgressBar - How???

    hmmmmm, seems like the thread is working properly, because when I choose a huge file and the loop is done, I see the progressbare like 10% full. But the problem is I dont see it filling up during the loop, I see it go from 0% to 10%. Do I need to put in a form refresh? or a progressbar refresh? Im using netbeans, FYI


    and yes the joptionpane message worked

  6. #6
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: jProgressBar - How???

    Doesn't matter what IDE you're using.
    What is you're OS?
    And are you sure your the loop is iterating 100 times?
    if not. Set the max of the progressbar to the appropriate value
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  7. #7

    Thread Starter
    Hyperactive Member xxarmoxx's Avatar
    Join Date
    Mar 2007
    Posts
    378

    Re: jProgressBar - How???

    Im using mac OS X 10.4.10.

    Im using that same exact code as above, as of now the progressbar and the loop dont share any values. I just want to get the simple case working before I make it more complicated. Simply have the progressbar move up 1 second at a time while the loop is processing.

    Will it help if I post the entire class here? Its not very big

  8. #8
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: jProgressBar - How???

    Not really
    If you're using MAC OS you'll have to make sure what version of Java you should be using. Because java have some compatibility issues with MAC
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  9. #9

    Thread Starter
    Hyperactive Member xxarmoxx's Avatar
    Join Date
    Mar 2007
    Posts
    378

    Re: jProgressBar - How???

    Forget about all that for a sec, check this out:

    Code:
      private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
    
    
            int count=0;
                            while (count < 7) {
                        try {
                            Thread.sleep(1000);
                            //updateThread.setPriority(NUMCOUNTERS+2);
                            jProgressBar1.setValue(count);
                            jProgressBar1.updateUI();
                        } catch (InterruptedException e) {
                        }
                        count++;
                    }
    }


    Try that, how come I dont see the progressbar raising? All I see is it goes from 0% to 7%

  10. #10
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: jProgressBar - How???

    because the count is iterating through 1-7 out of 100%
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  11. #11

    Thread Starter
    Hyperactive Member xxarmoxx's Avatar
    Join Date
    Mar 2007
    Posts
    378

    Re: jProgressBar - How???

    let me try again:

    Code:
        private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
    // TODO add your handling code here:
            
            //String ans;
           // ans = JOptionPane.showInputDialog(null, "Speed in miles per hour?");
            int count=0;
                            while (count < 100) {
                        try {
                            Thread.sleep(50);
                            //updateThread.setPriority(NUMCOUNTERS+2);
                            jProgressBar1.setValue(count);
                            jProgressBar1.updateUI();
                        } catch (InterruptedException e) {
                        }
                        count++;
                    }
      }

    how come I dont see the progressbar raising as it goes through the loop? All I see is it goes from 0% to 100% when the loop is finished

  12. #12
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: jProgressBar - How???

    you're passing 50 milliseconds to the Thread.sleep method, and I still think it's a MAC compatibility issue
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

Posting Permissions

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



Click Here to Expand Forum to Full Width