JavaFX:如何强制直接应用新的样式类?

时间:2016-03-22 11:43:37

标签: java javafx javafx-2 listener javafx-8

我正在构建一个应用程序,它在图形上显示不同寻路算法的结果。因此,每次扩展节点时,我都希望它被突出显示。出于这个原因,我有一个监听器,每次扩展节点时都会执行该监听器。此外,还有可能推迟这一过程。

但是新的样式类似乎只在算法完成后呈现。 另一方面,控制台输出在运行时正确显示。

有问题的代码:

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
        }
    }
};

示例输出:

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

1 个答案:

答案 0 :(得分:2)

您观察到的行为很正常。您正在设置新样式,但它们不会在下一个脉冲之前执行。但是因为您使用代码阻止应用程序线程,所以下一个脉冲将被延迟,直到完成为止。