0

the main problem is that I don't know how long my method will take to finish, I have 2 threads, one to execute the method that do stuff and the other one excute the progress of the progress bar, this is my first thread:

@Override
public void run() {

//This is a static variable, indicate when the method analyzeSentence finish
    finish = false; 

    String sentence = analyzeSentence();
    try {
        Thread.sleep( 1000 );
    } catch (InterruptedException e){
        System.err.println( e.getMessage() );
    }
    finish = true;
}

And in my second thread (the progress bar thread) I have this:

@Override
public void run() {
    i = 1;
    while(!Analyze.finish) {
        i = (i > 100) ? 1 : i+1; //Here I have the problem

        progressBar.setValue(i);
        progressBar.repaint();
        try {
            Thread.sleep(this.value);
        } catch (InterruptedException e) {
            System.err.println(e.getMessage());
        }

        if(Analyze.finish) {
            progressBar.setValue(100);
            break;
        }
    }
}

The problem is that the progress bar reset their values to zero but the method still not over so the progress bar is filled again and again... Is there any way to obtain the lenght of the time that the method takes to finish?

  • 1
    1) GUI updates should be done from the Event Dispatch Thread. 2) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 3) See [`JProgressBar.setIndeterminate(boolean)`](https://docs.oracle.com/javase/8/docs/api/javax/swing/JProgressBar.html#setIndeterminate-boolean-) .. – Andrew Thompson Apr 24 '15 at 03:44
  • *"Is there any way to obtain the lenght of the time that the method takes to finish?"* Computer programs don't know the future. ; ) You'd need to split your task up in to some known number of parts. Since we don't know what you're doing yet, we can't offer advice on how to do that. – Radiodef Apr 24 '15 at 03:47
  • @Bruno.. Since you don't know how much time your method will take to finish, its better if you use indeterminate JProgressbar... – NewBee Developer Apr 24 '15 at 04:23
  • Yep I already check the method, it works fine and with some details like @Radiodef said about split the task I can show when a part of the process ends, looks good. Thank you all for the answers :) – Bruno Arana Roa Apr 24 '15 at 04:54

0 Answers0