?
Solved

Problem With JProgressBar

Posted on 2006-05-24
2
Medium Priority
?
297 Views
Last Modified: 2010-04-17
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
0
Comment
Question by:sree032397
2 Comments
 
LVL 1

Accepted Solution

by:
rongasa earned 2000 total points
ID: 16749769
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
 
LVL 1

Author Comment

by:sree032397
ID: 16758580
Thank You,
It is working fine
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Loops Section Overview

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question