0

hi I have these lines of codes in java netbeans which save the result of p2 in Result.txt

System.setOut(new PrintStream(new FileOutputStream("Result.txt")));
String line1;
Process p2= Runtime.getRuntime().exec(command);
BufferedReader in = new BufferedReader(new InputStreamReader(p2.getInputStream()) );
while ((((line1 = in.readLine()) != null))||i<101)
{

   System.out.println(line1);
   i=i+10;
   jProgressBar1.setValue(i);

}
in.close();

JOptionPane.showMessageDialog(null,"successfull");

The code shows me the progress bar only when i=100 that is when the progress bar is filled and the while loop is completed.

I want the code to show me progress bar at 10,20,30,40,50,60,70,80,90,100 while saving.

Thank you.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
vidzz
  • 197
  • 2
  • 4
  • 15
  • Show us the code where jProgressBar1 is created. – gkalpak Jun 02 '13 at 08:10
  • Check out [this](http://stackoverflow.com/questions/14113644/java-global-reusable-loading-dialog/14114663#14114663) example – MadProgrammer Jun 02 '13 at 08:22
  • You might find [ProgressMonitorInputStream](http://docs.oracle.com/javase/7/docs/api/javax/swing/ProgressMonitorInputStream.html) for some help, so long as you're not reading the file within the EDT – MadProgrammer Jun 02 '13 at 08:49

3 Answers3

2

You're doing a lengthy operation in the event dispatch thread, which prevents it from doing its job (painting, and thus displaying the progress in the progress bar) until the lengthy operation is finished.

Do this lengthy operation in a separate thread, using a SwingWorker for example. And read the Swing tutorial on concurrency to understand more about threads and swing.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • How did yu come to the conclusion he is doing a lengthy operation in the event dispatch thread ??? – gkalpak Jun 02 '13 at 08:25
  • 1
    I came to this conclusion because that's what the code is doing: it reads the output of an external process, which takes time to complete (hence the need of a progress bar). The symptoms of the problem are very clear and usual (GUI frozen until the loop completes), and this question is asked at least twice a week, if not once per day. If this was done in a separate thread, the code would be buggy, since it uses a swing component out of the EDT. And I guess the OP would have said it. – JB Nizet Jun 02 '13 at 08:28
  • We could be in any thread other than EDT. It is an assumption (probably valid. but certainly not related to any clues provided in the question). Besides, if you feel this question has been answered before, why not marking it as duplicate instead of answering it ? – gkalpak Jun 02 '13 at 08:32
  • Hey, why don't you delete your completely wrong answer instead of criticizing mine? – JB Nizet Jun 02 '13 at 08:38
  • Because I am not convinced yours is any more useful than mine yet :) I will as soon as this becomes the case (example after OP has updated his question with more info). Thx for the suggestion ! – gkalpak Jun 02 '13 at 08:40
  • 1
    I'll explain why your answer is wrong then. Either your code runs in the EDT, and it will freeze the GUI until the process is done, and the progress bar is at 100% (which is exactly what the OP is seeing). Or it runs out of the EDT, and you're updating a Swing component from outside of the EDT, which is illegal. In any case, the answer is wrong. Contrast this to mine, which has 99% chances of being right. – JB Nizet Jun 02 '13 at 08:44
0

in my eclipse ,you code can correctly show in debug mode.And I think your question is "i=i+10;" can run quickly than it show.You can set the command show more i = i+1.May be you can get you want.

Chain HO
  • 26
  • 1
  • 3
-2

Use validate method to repaint the progress bar after update

 while ((((line1 = in.readLine()) != null))||i<101)
    {

   ...
       jProgressBar1.setValue(i);
       jProgressBar1.repaint()
    }
Muhannad A.Alhariri
  • 3,702
  • 4
  • 30
  • 46