Label does not update via setText in Javafx

Asked

Viewed 136 times

1

Good night,

I have a problem trying to change the text of a label through the sexText method and I can’t identify the problem, I appreciate the help. Code:

void atualizar()
{

    lbStatusPlatao.setText(jantar.statusFilosofo(0).toString());
    lbStatusSocrates.setText(jantar.statusFilosofo(1).toString());
    lbStatusRousseau.setText(jantar.statusFilosofo(2).toString());
    lbStatusMarx.setText(jantar.statusFilosofo(3).toString());
    lbStatusAristoteles.setText(jantar.statusFilosofo(4).toString());


    if(jantar.statusGarfo(0))
    {
        statusGarfo1.setFill(javafx.scene.paint.Color.GREEN);
    }
    else
    {
        statusGarfo1.setFill(javafx.scene.paint.Color.RED);
    }

    if(jantar.statusGarfo(1))
    {
        statusGarfo2.setFill(javafx.scene.paint.Color.GREEN);
    }
    else
    {
        statusGarfo2.setFill(javafx.scene.paint.Color.RED);
    }

    if(jantar.statusGarfo(2))
    {
        statusGarfo3.setFill(javafx.scene.paint.Color.GREEN);
    }
    else
    {
        statusGarfo3.setFill(javafx.scene.paint.Color.RED);
    }

    if(jantar.statusGarfo(3))
    {
        statusGarfo4.setFill(javafx.scene.paint.Color.GREEN);
    }
    else
    {
        statusGarfo4.setFill(javafx.scene.paint.Color.RED);
    }

    if(jantar.statusGarfo(4))
    {
        statusGarfo5.setFill(javafx.scene.paint.Color.GREEN);
    }
    else
    {
        statusGarfo5.setFill(javafx.scene.paint.Color.RED);
    }
}

@FXML
void comecarJantar(ActionEvent event) throws InterruptedException 
{
    jantar.criarFilosofos(cbDeadlock.isSelected());
    jantar.iniciar();

    Task<Void> taskAtualizacao = new Task<Void>()
    {
        @Override
        protected Void call() throws Exception 
        {
            while(true)
            {
                atualizar();
                Thread.sleep(6000);
            }
        }
    };

    Thread atualizador = new Thread(taskAtualizacao);
    atualizador.start();
}

@FXML
void pararJantar(ActionEvent event) 
{
    jantar.parar();
}

Exceptions generated:

Gtk-Message: 02:52:19.263: Failed to load module "Canberra-gtk-module" Exception in thread "Thread-8" java.lang.Illegalstateexception: Not on FX application thread; currentThread = Thread-8 at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:279) at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(Quantumtoolkit.java:423) at javafx.scene.Parent$2.onProposedChange(Parent.java:367) at com.sun.javafx.collections.Vetoablelistdecorator.setAll(Vetoablelistdecorator.java:113) at com.sun.javafx.collections.Vetoablelistdecorator.setAll(Vetoablelistdecorator.java:108) at com.sun.javafx.scene.control.skin.Labeledskinbase.updateChildren(Labeledskinbase.java:575) at com.sun.javafx.scene.control.skin.Labeledskinbase.handleControlPropertyChanged(Labeledskinbase.java:204) at com.sun.javafx.scene.control.skin.Labelskin.handleControlPropertyChanged(Labelskin.java:49) at com.sun.javafx.scene.control.skin.Behaviorskinbase.lambda$registerChangeListener$61(Behaviorskinbase.java:197) at com.sun.javafx.scene.control.Multiplepropertychangelistenerhandler$1.changed(Multiplepropertychangelistenerhandler.java:55) at javafx.beans.value.WeakChangeListener.changed(Weakchangelistener.java:89) at com.sun.javafx.binding.Expressionhelper$Singlechange.fireValueChangedEvent(Expressionhelper.java:182) at com.sun.javafx.binding.Expressionhelper.fireValueChangedEvent(Expressionhelper.java:81) javafx.beans.Property.StringPropertyBase.fireValueChangedEvent(Stringpropertybase.java:103) javafx.beans.Property.StringPropertyBase.markInvalid(Stringpropertybase.java:110) javafx.beans.Property.StringPropertyBase.set(Stringpropertybase.java:144) javafx.beans.Property.StringPropertyBase.set(Stringpropertybase.java:49) javafx.beans.Property.StringProperty.setValue(Stringproperty.java:65) at javafx.scene.control.Labeled.setText(Labeled.java:145) at controller.MesaController.update(Mesacontroller.java:58) at controller.Mesacontroller$1.call(Mesacontroller.java:124) at controller.Mesacontroller$1.call(Mesacontroller.java:1) javafx.concurrent.Task$Taskcallable.call(Task.java:1423) at java.util.Concurrent.FutureTask.run(Futuretask.java:266) at java.lang.Thread.run(Thread.java:748) Exception in thread "Thread-8" java.lang.Illegalstateexception: Not on FX application thread; currentThread = Thread-8 at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:279) at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(Quantumtoolkit.java:423) at javafx.scene.Parent$2.onProposedChange(Parent.java:367) at com.sun.javafx.collections.Vetoablelistdecorator.setAll(Vetoablelistdecorator.java:113) at com.sun.javafx.collections.Vetoablelistdecorator.setAll(Vetoablelistdecorator.java:108) at com.sun.javafx.scene.control.skin.Labeledskinbase.updateChildren(Labeledskinbase.java:575) at com.sun.javafx.scene.control.skin.Labeledskinbase.handleControlPropertyChanged(Labeledskinbase.java:204) at com.sun.javafx.scene.control.skin.Labelskin.handleControlPropertyChanged(Labelskin.java:49) at com.sun.javafx.scene.control.skin.Behaviorskinbase.lambda$registerChangeListener$61(Behaviorskinbase.java:197) at com.sun.javafx.scene.control.Multiplepropertychangelistenerhandler$1.changed(Multiplepropertychangelistenerhandler.java:55) at javafx.beans.value.WeakChangeListener.changed(Weakchangelistener.java:89))

1 answer

2


If you read your exception, you will see that the message is "Not on FX application thread". This was because your setText() was called in a different thread than the one where the Javafx application is running. So what Java is telling you is that you can only change Javafx element properties from the main thread, that is, the one where the application is running. It’s easy to fix it. Change that chunk of your code to:

Platform.runLater(() -> {
    lbStatusPlatao.setText(jantar.statusFilosofo(0).toString());
    lbStatusSocrates.setText(jantar.statusFilosofo(1).toString());
    lbStatusRousseau.setText(jantar.statusFilosofo(2).toString());
    lbStatusMarx.setText(jantar.statusFilosofo(3).toString());
    lbStatusAristoteles.setText(jantar.statusFilosofo(4).toString());
});

So you are telling your Javafx application to run its changes as soon as possible.

  • Thank you very much, solved, thanks, I did not know that an external Thread does not have permission to change Javafx components, I’m starting with the API now, thank you very much for the information and help

Browser other questions tagged

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