Problem With JProgressBar

I have written a program which has a for loop which will be executed for 0 to 10000 or more. I have added a JProgressBar to monitor the progress. The problem is the ProgressBar is displayed after the completion of the execution.

How can I solve this.

Help me in this regard


Thanks
LVL 1
sree032397Asked:
Who is Participating?
 
rongasaConnect With a Mentor Commented:
You need to make a different thread that will do the process that you discribe and have the progress bar in the main program. I most of the times create a thread that uses the progressBar as well.
I attach you a code that i use...
//main...
  Mymsg waitmsg = new Mymsg();
// I usually work with sockets so i need to make new thread to do the process but...you do not need to

  public class Mymsg implements Runnable
{
  //  Frame f;
  JProgressBar pbar;
  static final int MY_MINIMUM=0;
  static final int MY_MAXIMUM=50;
  JPanel panel;
  JFrame frame;
  int i;
  Mymsg()
  {
  Thread t = new Thread(this);
  pbar = new JProgressBar();
  panel=new JPanel();
  pbar.setMinimum(MY_MINIMUM);
  pbar.setMaximum(MY_MAXIMUM);
  pbar.setVisible(true);
  panel.add(pbar);
  frame = new JFrame("Please wait while ...");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setContentPane(pbar);
  frame.setEnabled(false);
  frame.pack();
  frame.setBounds(300,300,300,50);
  frame.setVisible(true);
  t.start();
}
public void run()
{
  panel.setVisible(true);
  for (i = MY_MINIMUM; i <= MY_MAXIMUM; i++) {
    try{
      java.lang.Thread.sleep(200);
    }catch(Exception e){};
    final int percent = i;
    pbar.setValue(percent);
  }
}
  public void stop()
  {
    if (i!=MY_MAXIMUM)
    {
      for (; i <= MY_MAXIMUM; i++)
      {
        final int percent = i;
        try{
              java.lang.Thread.sleep(50);
       }catch(Exception e){};
          pbar.setValue(percent);
      }
    frame.dispose();
  }
}
}
// I guess you know java so you can understand how to adjust it in your code and how to put the process you need to do under progress bar.

I hope i helped.
0
 
sree032397Author Commented:
Thank You,
It is working fine
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.