刷新标签无法正常工作javafx

时间:2016-09-15 09:43:28

标签: javafx

我在Label上刷新时遇到问题。 我有这样的功能:

    public void majMontantPaye(Double montantPaye) {
    System.out.println("montant paye : "+montantPaye);

    setMontantPaye(this.montantPaye+montantPaye);

    Platform.runLater(() -> labelMontantPaye.setText(String.format("%.2f", this.montantPaye)+Messages.getMessage("0052")));
}

我的函数是由API调用的。此API与允许插入硬币的机器通信。我的功能必须显示机器中的总和插入。

问题是,当我在机器中同时插入大量硬币时,我的功能正确地调用了检测到的每一枚硬币,因此System.out.println("montant paye : "+montantPaye);正确显示每个检测到的硬币,但标签&# 34; labelMontantPaye"不会刷新检测到的每一枚硬币。刚刚结束总和。

我猜UI没有正确刷新,但我不知道如何正确刷新我的标签。

请帮忙,对不起,我是法国人。

1 个答案:

答案 0 :(得分:0)

您可以遵循以下逻辑:

  

如评论中所述:    使用Platform.runLater(...)将任务排入JavaFXThread。但是当你有很多事件时,#34;你只会看到最后的结果。 (也许以前的那些很短的时间)。

     

使用BlockingQueue存储插入的每个硬币。使用下面的方法(另请参阅可用方法的教程,这里我使用的是阻止当前线程的put,如果最多硬币被插入到队列中,如果您不希望此设置最大值为500.000):

public void insertCoin(//maybe the kind of coin){
      //add the coin into the BlockingQueue
      blockingQueue.put(//coin);
}
  

使用运行无限循环的Thread。线程正在唤醒   每次插入新硬币时,完成后,该线程   等待JavaFXThread刷新标签文本:

new Thread(() -> {

        //Run an infinity Thread
        while (true) {

            // Blocks until the queue has really any coins inserted
            blockingQueue.get();

            // Synchronize with javaFX thread
            CountDownLatch latch = new CountDownLatch(1);
            Platform.runLater(() -> {
                label.setText(....);
                latch.countDown();
            });

            // Block the Current Thread until the text is refreshed from
            // JavaFX Thread
            latch.await();

        }
}).start();