0

I have a strange error happening. Consider the code bellow and this AtualizadorVeiculos.getInstance().atualizaBanco() method.

AtualizadorVeiculos.getInstance().atualizaBanco(file.getPath(), (line, res, lineNumber) -> {
    DadoCarregadoTabela dado = new DadoCarregadoTabela(line, res);
    int percentual = (int)(lineNumber/(double)numberOfLines * 100);
    this.lblPercentual.setText(percentual + "%");
    this.tblCarregados.getItems().add(dado);
    this.pgBar.setProgress(lineNumber/(double)numberOfLines);
});

Notice that I pass a listener as second parameter; this is called by another JavaFx thread inside the method, many times, when some event occur. When it is called I update 3 GUI components: the lblPercentual (Label), tblCarregados (Table), and the pgBar (Progressbar).

The strange thing is that both the table and the progress bar update correctly, but not the label! If I remove its line, things work perfectly. To make it stranger, the exception only runs when the whole processing is finished and the progress bar is full.

I'm lost.

The atualizaBanco method.

public void atualizaBanco(String filePath, AtualizacaoListener listener) {
    final ScheduledExecutorService scheduler = Executors
            .newScheduledThreadPool(1);
    scheduler.schedule(new Runnable() {
        @Override
        public void run() {
            String line = "";
            int lineNumber = 0;
            try {
                FileReader inputFile = new FileReader(filePath);
                BufferedReader bufferReader = new BufferedReader(inputFile);
                while ((line = bufferReader.readLine()) != null) {
                    lineNumber++;
                    setDados(line);
                    listener.dadoCarregado(line, true, lineNumber);
                }
                dropListeners();
                bufferReader.close();
            } catch (Exception e) {
                System.out.println("Error while reading file line by line:" + e.getMessage());
                listener.dadoCarregado(line, false, lineNumber);
            }
        }
    }, 10, TimeUnit.MILLISECONDS);
}

The error:

Exception in thread "pool-4-thread-1" java.lang.IllegalStateException: Not on FX application thread; currentThread = pool-4-thread-1`
  • It's what the error says: "Not on FX application thread". Use Platform.runLater(). – Roland Jun 12 '15 at 03:06
  • That might work, but doesn't explain why it updates the other two components. I've been using the scheduler for some time. Never had problems. – Jemerson Damásio Jun 12 '15 at 03:10
  • As @Roland says *all* updates to the UI ***must*** be performed on the FX Application Thread, regardless if not doing so throws an exception. The FX library will check this rule isn't broken and throw an exception any time it can do so without affecting performance, but even if it doesn't it is a mistake to break this rule. – James_D Jun 12 '15 at 03:32
  • After you guys help, I managed to get the solution using a `Platform.runLater` inside the listener who updates the components in fact. Thanks for that. – Jemerson Damásio Jun 12 '15 at 03:49

0 Answers0