JavaFX: How to enforce that a new style class is applied directly?

I am building an application which displays the results of different pathfinding algorithms on a graph. So every time a node is expanded i want it to be highlighted. For this reason i have a listener which is executed each time a node gets expanded. In addition there is the possibility to delay the process.

But the new style classes seem only to be rendered after the algorithm finishes. On the other hand the console output is displayed correctly at runtime.

The code in question:

IDiGraphSearchListener<XYNode, CostEdge<XYNode>> listener = new IDiGraphSearchListener<XYNode, CostEdge<XYNode>>() {
    List<CostEdge<XYNode>> way;
    @Override
    public void onExpandNode(XYNode node, List<CostEdge<XYNode>> way) {
        if (this.way == null) {
            this.way = way;
        }
        System.out.println("Expanding");
        for (CostEdge<XYNode> costEdge : way) {
                graphPane.lookup("#"+costEdge.getId()).getStyleClass().add("visited");
                graphPane.lookup("#"+costEdge.getSource().getId()).getStyleClass().add("visited");
                graphPane.lookup("#"+costEdge.getTarget().getId()).getStyleClass().add("visited");
                System.out.println("Coloring-> EdgeID:"+costEdge.getId()+" From:"+costEdge.getSource().getId()+" To:"+costEdge.getTarget().getId());
        }       
        try {
            System.out.println("Sleeping");
            Thread.sleep((long) (pathfindingSpeedSlider.getValue()*1000));
        } catch (InterruptedException ex) {
            //ToCatchOrNot
        }
    }
};

Sample output:

Algo: AStar
Expanding
Expanding
Coloring-> EdgeID:id20 From:id1 To:id2
Coloring-> EdgeID:id25 From:id1 To:id4
Coloring-> EdgeID:id45 From:id1 To:id9
Expanding
Coloring-> EdgeID:id21 From:id2 To:id3
Coloring-> EdgeID:id20 From:id1 To:id2
Coloring-> EdgeID:id25 From:id1 To:id4
Coloring-> EdgeID:id45 From:id1 To:id9
Expanding
Coloring-> EdgeID:id21 From:id2 To:id3
Coloring-> EdgeID:id20 From:id1 To:id2
Coloring-> EdgeID:id25 From:id1 To:id4
Coloring-> EdgeID:id45 From:id1 To:id9
Expanding
Coloring-> EdgeID:id21 From:id2 To:id3
Coloring-> EdgeID:id20 From:id1 To:id2
Coloring-> EdgeID:id46 From:id9 To:id5
Coloring-> EdgeID:id25 From:id1 To:id4
Coloring-> EdgeID:id45 From:id1 To:id9
Expanding
Coloring-> EdgeID:id21 From:id2 To:id3
Coloring-> EdgeID:id20 From:id1 To:id2
Coloring-> EdgeID:id46 From:id9 To:id5
Coloring-> EdgeID:id25 From:id1 To:id4
Coloring-> EdgeID:id28 From:id5 To:id6
Coloring-> EdgeID:id45 From:id1 To:id9
Expanding
Coloring-> EdgeID:id21 From:id2 To:id3
Coloring-> EdgeID:id20 From:id1 To:id2
Coloring-> EdgeID:id46 From:id9 To:id5
Coloring-> EdgeID:id25 From:id1 To:id4
Coloring-> EdgeID:id30 From:id6 To:id7
Coloring-> EdgeID:id28 From:id5 To:id6
Coloring-> EdgeID:id35 From:id6 To:id11
Coloring-> EdgeID:id45 From:id1 To:id9
getWay1
->id1/id9->id9/id5->id5/id6->id6/id7getWay2
Finish

Answers


The behaviour you observe is quite normal. You are setting new styles but they will not be executed before the next pulse. But because you are blocking the application thread with your code the next pulse will be delayed until you are done.


Need Your Help

Why isn't a function's arguments object an array in Javascript?

javascript function arrays arguments

Since it seems like the first thing people do is convert arguments into a real array, I'm interested in why the Javascript language authors and implementers decided, and continue to think, that arg...

How to create Custom Views

android view android-custom-view custom-view

Hello, how can I create a custom view for my app? Observed examples of Google, but could not get it right ... if anyone know any tutorial on this subject I thank! Good night!