A
A
Alexey Chizhov2015-10-26 09:24:48
Java
Alexey Chizhov, 2015-10-26 09:24:48

How to correctly organize the transmission of an event from a separate thread to the GUI on JavaFx?

I have a MainController and a UDPServer class that spins in a separate thread and waits for a message, when a message arrives I need to display it on the GUI. How to organize such interaction?

public class UDPServer implements Runnable {

    // ...

    @Override
    public void run() {

        // ...

        if(message.equals("N1-M")){

            // пришло сообщение и нужно отобразить его в GUI
            // в моем случае отобразить значит изменить цвет vRectangle            
        }

        // ...
    }
}


public class MainController {

    // ...

    @FXML Rectangle vRectangle;

    // ...
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
programmerjava, 2015-10-26
@programmerjava

When you need to perform some action on the JavaFx thread, you need to call Platform.runLater(Runnable r). Those. In the argument, you specify the action that will then be performed. The action is non-blocking and asynchronous. Waiting for the end of this action is already a more difficult task and quite another.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question