2013-08-22 62 views
0

我在盯着我的代碼,我很困擾一個問題: 我想淡出過渡,我想阻止當前線程,while淡入淡出過渡正在運行。 所以,我的嘗試是創造的CountDownLatch,它阻止該線程,直到transition.setOnFinished()被調用,在這裏我做一個latch.countdown()。簡而言之:我想確保轉換總是可見的。JavaFX FadeTransition.onSetOnFinished with CountdownLatch無法正常工作

看起來很直截了當,但... setOnFinished()不會被調用,因爲上面提到的當前線程被倒計數鎖存器阻塞。

我該如何解決這個問題? Thx提前。

private void initView() { 
     Rectangle rect = new Rectangle(); 
     rect.widthProperty().bind(widthProperty()); 
     rect.heightProperty().bind(heightProperty()); 
     rect.setFill(Color.BLACK); 
     rect.setOpacity(0.8f); 

     getChildren().add(rect); 

     MyUiAnimation animator = new MyUiAnimation(); 
     fadeInTransition = animator.getShortFadeInFor(this); 

     fadeOutTransition = animator.getShortFadeOutFor(this); 
     fadeOutTransition.setOnFinished(new EventHandler<ActionEvent>() { 
      @Override 
      public void handle(ActionEvent arg0) { 
       Platform.runLater(new Runnable() { 
        @Override 
        public void run() { 
         latch.countDown(); 
         setVisible(false); 
        } 
       }); 
      } 
     }); 
    } 

public void hide() { 
     fadeInTransition.stop(); 

     if (isVisible()) { 
      latch = new CountDownLatch(1); 
      fadeOutTransition.playFromStart(); 
      try { 
       latch.await(); 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    } 

回答

0

您只能修改和查詢JavaFX應用程序線程上的活動場景圖。所有代碼都可以與場景圖形一起使用,因此它們都必須在JavaFX應用程序線程上運行。如果JavaFX應用程序線程中的所有內容都已經運行,那麼在您的代碼中沒有任何原因會導致併發性相關的結構。

如果使用阻塞調用像latch.await(),你將阻止JavaFX應用程序的線程,這將阻止任何渲染,佈局或動畫步驟來運行。 CountdownLatch不應在此上下文中使用,應從代碼中刪除。

調用Platform.runLater是不必要的,因爲它的目的是在JavaFX應用程序線程上運行代碼,並且您已經在JavaFX應用程序線程中。 Platform.runLater不應在此上下文中使用,應從代碼中刪除。