progress bar is not updating correctly

Asked

Viewed 147 times

1

I’m developing a data synchronizer, where every five seconds I check for data being synchronized. The synchronization part with the check time every 5 seconds is already working. However, I could not make the progress bar track the progress of the Task, ie, go from 0 to 100 every loop iteration. Follow my code:

 public void timerSincronizacao05Secs() {
    Task sincronizacaoTask05Secs = new Task<Void>() {
        @Override
        protected Void call() throws Exception {
            while (isRunning == false) {
                isRunning = true;
                System.out.println("iniciou  em.....: " + new Date());
                fachada.sincronizarProdutos();
                barraProgresso.setProgress(0.50);
                fachada.sincronizarClientes();
                barraProgresso.setProgress(barraProgresso.getProgress() + 0.50);
                System.out.println("terminou em.....: " + new Date());
                isRunning = false;
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException ex) {
                    break;
                }
            }
            return null;
        }
    };

    lblteste.textProperty().bind(sincronizacaoTask.messageProperty());
    Thread threadSinv05Secs = new Thread(sincronizacaoTask05Secs);
    threadSinv05Secs.setName("Thread sincronização a cada 5 segundos");
    threadSinv05Secs.setDaemon(true);
    threadSinv05Secs.start();
}

I know it’s not the best way to go, but if anyone can help, I’d appreciate it!

  • 1

    Like, at the end of the process it did not Zera the bar again, I tried tbm put a label to inform you what is being updated but tbm did not work. And I don’t know if that’s the way I did it.

1 answer

1


The problem is that you gave a bind to the messageProperty in the thread without using the upgrade methods that the thread class offers: updateMessage(), updateProgress(), updateTitle(), updateValue().

The correct way to connect thread progress to bar progress is this way:

ProgressBar progressbar = new ProgressBar(0.0);

// [...] Algum código

Task<Void> task = new Task<Void>() {
    @Override
    protected Void call() throws Exception {
        // Método isCancelled() retorna true quando task.cancel() é chamado
        while(!isCancelled()){
            for (int i = 0; i < 10000000; i++){
                // updateProgress(workdone,totalwork) valor numérico
                // para trabalho já realizado e trabalho total 
                updateProgress(i, 10000000);
            }
            Thread.sleep(5000);
        }
        return null;
    }
};
progressbar.progressProperty().bind(task.progressProperty());

The code wasn’t even supposed to work since changes in the GUI can only be made by FX Thread (maybe that’s why the bug). Your code could look like this:

updateMessage("iniciou  em.....: " + new Date());
fachada.sincronizarProdutos();
updateProgress(50,100);
fachada.sincronizarClientes();
updateProgress(100,100);
updateMessage("terminou em.....: " + new Date());

// [...] 
lblteste.textProperty().bind(sincronizacaoTask.messageProperty());
progressbar.progressProperty().bind(sincronizacaoTask.progressProperty());
  • 1

    Thank you @Gustavofragoso, I decided to take some of your logic. the bar is working perfectly. The only thing I still could not manage was to update the label on the screen. I’m still trying, when I get put.

  • I solved it, the problem was I hadn’t given one new on the label that would receive the update. Thank you.

  • Glad you could help :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.