Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4393042

JProgressBar does not update if maximum value is very large

XMLWordPrintable

    • beta
    • x86
    • windows_98



      Name: yyT116575 Date: 11/28/2000


      java -version
      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)


      If the maximum value for a JProgressBar is very large (close to
      Integer.MAX_VALUE), then setValue on the JProgessBar does not cause a repaint.
      This can be seen in the following example code. When it is run the progress
      bar should go up in increments of 10% each second, but it does not. That
      adjoining JLabel displying the value actually supplied to the progress bar
      *does* correctly update.

      If the value of the constant MAXIMUM is reduced to, say, Integer.MAX_Value / 2,
      then all works as it should. Alternatively, calling setMaximum on a small number
      and then again on the large value before calling setValue also causes it to
      repaint correctly. Or simply calling repaint() explicitly fixes it, too.

      Note that while it sounds similar, the symptoms of this bug are different than
      those of #4106139 already in the database, which is closed as fixed.

      Besides Windows, I have also seen identical behavior under linux.

      Sample code:

      import java.util.Timer;
      import java.util.TimerTask;
      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;

      public class ProgressBug extends JFrame {

          private static final int MAXIMUM = Integer.MAX_VALUE - 100;
          private static final int GRANULARITY = MAXIMUM / 10;

          private JProgressBar progressBar = new JProgressBar(0, MAXIMUM);
          private JLabel progressLabel
              = new JLabel(Integer.toString(MAXIMUM));
          private Timer progressTimer = new Timer();
          
          private ProgressBug() {
              getContentPane().add(progressBar, BorderLayout.WEST);
              progressBar.setStringPainted(true);
              getContentPane().add(progressLabel, BorderLayout.EAST);
              addWindowListener(new WindowAdapter() {
                      public void windowClosing(WindowEvent evt) {
                          System.exit(0);
                      }
                  });
              setLocation(400, 300);
              pack();
              progressLabel.setText("");
          }

          public static void main(String[] args) {
              (new ProgressBug()).run();
          }

          private class UpdateTask extends TimerTask {

              final int value;

              UpdateTask(int v) {
                  value = v;
              }

              public void run() {
                  EventQueue.invokeLater(new Runnable() {
                          public void run() {
                              // Adding the following two lines here will
                              // make this work.
                              // progressBar.setMaximum(1);
                              // progressBar.setMaximum(MAXIMUM);
                              progressBar.setValue(value);
      // Or adding the following one line here will also
      // make this work.
      // progressBar.repaint(100);
                              progressLabel.setText(Integer.toString(value));
                          }
                      });
              }
          }

          private void run() {
              setVisible(true);
              for (int i = 0; i <= 10; ++i) {
                  progressTimer.schedule(new UpdateTask(i * GRANULARITY), i * 1000);
              }
          }

      }
      (Review ID: 112864)
      ======================================================================

            apikalev Andrey Pikalev
            yyoungsunw Yung-ching Young (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: