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

JProgressBar behaves abnormal when the maximun value is a large number

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P2 P2
    • 1.2.0
    • 1.2.0
    • client-libs
    • swing1.0fcs
    • sparc
    • solaris_2.5
    • Verified

      This bug has been verified in swing-pre-1.0 and jdk1.1.5 by Patience Chu.

      "Hi,

      I use Swing's JProgressBar in my MPEG analyzer to display progress
      relative to the total size of the MPEG stream. I have a case where
      MPEG file is about 50MB. In this case, I set minimum to 0 and maximum
      to 50000000. When the progress reaches about 20% progress display
      disappears and reappears after another 20%.

      I am attaching sample code to reproduce the prblem.

      Thanks,
      ----
      Venkat Tipparam
      Network Products Group
      Sun Microsystems.
      ###@###.###
      (650) 688-9410
      "
      import java.awt.*;
      import java.awt.event.*;
      import com.sun.java.swing.*;

      public class ProgressBarBug extends JFrame implements ActionListener, Runnable {
          int maxValue;
          JTextField textField;
          JProgressBar progressBar;
          JButton startButton;
          Thread thread;
          boolean stopThread = false;
          
          public ProgressBarBug(int maxValue) {
              this.maxValue = maxValue;
              getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
              textField = new JTextField(10);
              textField.setMaximumSize(textField.getPreferredSize());
              textField.setAlignmentX(LEFT_ALIGNMENT);
              progressBar = new JProgressBar();
              progressBar.setAlignmentX(LEFT_ALIGNMENT);
              progressBar.setMinimum(0);
              progressBar.setMaximum(maxValue);
              progressBar.setValue(0);
              startButton = new JButton("Start");
              startButton.setAlignmentX(LEFT_ALIGNMENT);
              startButton.addActionListener(this);
              getContentPane().add(textField);
              getContentPane().add(progressBar);
              getContentPane().add(startButton);
          }

          public void actionPerformed(ActionEvent e) {
              if (e.getSource() == startButton) {
                  if (thread != null) {
                      stopThread = true;
                      thread = null;
                  }
                  stopThread = false;
                  thread = new Thread(this);
                  thread.start();
              }
          }
          
          public void run() {
              int value = 0;
              int inc = maxValue / 1000;
              if (inc <= 0) {
                  inc = 1;
              }
      System.out.println( "ProgressBar Max = " + progressBar.getMaximum());
              while ((value < progressBar.getMaximum()) && !stopThread) {
                  value += inc;
                  progressBar.setValue(value);
                  textField.setText(String.valueOf(value));
                  try {
                      Thread.sleep(5);
                  } catch (InterruptedException e) {
                      //
                  }
              }
          }

          public static void main(String[] args) {
            int max = 50000000;
            System.out.println( max ) ;
              if (args.length > 0) {
                  max = Integer.parseInt(args[0]);
              }
              ProgressBarBug f = new ProgressBarBug(max);
              f.setSize(200, 100);
              f.show();
          }
      }

            stewilso Steve Wilson (Inactive)
            pchusunw Patience Chu (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: