4 Replies Latest reply on Oct 8, 2004 8:45 PM by 807587

    How to slow things down to see the progress bar?

    807587
      how to slow things down to let the progress bar to show its progress?

      everytime i run the program i can see only two scenario: 1. 0% 2. 100%

      i think the stuffs are too little to make any difference.

      what should i add in the codes so that i can see the progress?

      thanks!

      here is my codes:

      jProgressBar1.setValue(1);
      .....
      do some stuff here

      jProgressBar1.setValue(15);
      .....
      do some stuff

      jProgressBar1.setValue(31);
      .....
      do some stuff

      jProgressBar1.setValue(41);
      .....
      do some stuff

      jProgressBar1.setValue(61);
      .....
      do some stuff
      jProgressBar1.setValue(100);
        • 1. Re: How to slow things down to see the progress bar?
          807587
          http://forum.java.sun.com/thread.jsp?forum=57&thread=460538
          • 2. Re: How to slow things down to see the progress bar?
            807587
            Hi!

            When you call update on the progress bar, the value of the progress bar is updated, but it won't be reflected on the screen until Swing has a chance to repaint. Swing can't repaint until your code is finished. So, what's happening is something like this:

            setValue(1), do stuff, setValue(15), do stuff, setValue(31), do stuff, Swing Repaint

            Also, not only does this block the progress bar from updating, but as long as your code here is running it blocks all of Swing (repainting, handling events, etc...).

            If your "stuff" isn't very time consuming, just remove the progress bar altogether. Otherwise, to make things work properly, you'll want to move it to another thread. Then, from that thread you can keep the progress bar up to date with:
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    jProgressBar1.setValue(xxx);
                }
            });
            Hope that helps!
            Shannon Hickey (Swing Team)
            • 3. Re: How to slow things down to see the progress bar?
              807587
              Shannon, wouldn't it be a good idea if JProgressBar's setValue were thread safe, like JTextArea's setText?
              After all, if JProgressBar were being used correctly, it's likely being updated from a non-EDT. Afternately,
              there could be a new setting method that is thread-safe.

              (BTW, check out the code I gave in the link above. I find it handy to do thinks like that -- putting
              a runnable in the event queue that can be updated while it waits. For example, you may have a label
              that shows the current state of an ongoing operation (file names as you walk a directory structure?)
              and you want to update the label's text without flooding the event queue.)
              • 4. Re: How to slow things down to see the progress bar?
                807587
                I'm sorry, but the logic of the question has escaped me.

                You want to slow your program down in order to watch the lights on a progress bar grow from one end to the other?

                If the process you're watching happens fast enough that there's no time for a progress bar to paint, why have the progress bar at all?

                Dave Hall
                http://jga.sf.net