Philip94
Philip94

Reputation: 17

Platform.runLater() with Lambda Expression not working

In my program, I often make modifications to the UI using another thread. The changes look like that:

buffer.add(new Object[]{message.getSecondNode().getUINode(), "red"});

Therefore I buffer these modifications in order to not overload the UI. But in the following method the program does not make all the changes delivered in the buffer.

private void changeColor(List<Object[]> buffer) {
    Platform.runLater(() -> {
        for (Object[] object : buffer) {
            if (object[0] instanceof UIEdge) {
                UIEdge edge = (UIEdge) object[0];
                edge.setColor((String) object[1]);
            } else if (object[0] instanceof UINode) {
                if ((String) object[1] == "red")
                    Util.print("");
                UINode node = (UINode) object[0];
                node.getEllipse().setFill(Paint.valueOf((String) object[1]));
            }
        }
    });
}

In the following picture you see that the buffer has a different size in the method to its global size in the program. Does anyone know why?

Upvotes: 0

Views: 2539

Answers (1)

Steve
Steve

Reputation: 1041

you might want to consider using JavaFX Service and Task instead of a buffer to make updates. These are JavaFX classes that are provided to make multi-threading easier in a JavaFX application.

https://docs.oracle.com/javase/8/javafx/api/javafx/concurrent/Task.html

Because the Task is designed for use with JavaFX GUI applications, it ensures that every change to its public properties, as well as change notifications for state, errors, and for event handlers, all occur on the main JavaFX application thread.

https://docs.oracle.com/javase/8/javafx/api/javafx/concurrent/Service.html

The Service by default uses a thread pool Executor with some unspecified default or maximum thread pool size. This is done so that naive code will not completely swamp the system by creating thousands of Threads.

Here's a brief tutorial if your not already familiar with them. https://docs.oracle.com/javase/8/javafx/interoperability-tutorial/concurrency.htm

Upvotes: 1

Related Questions