2014-02-10 51 views
7

我試圖顯示一個標籤,說所有在JRE 8中只有3秒正確,因爲我無法在JRE 7中使用DatePicker,並且我收回了此錯誤。計時器錯誤java.lang.IllegalStateException

Exception in thread "Timer-2" java.lang.IllegalStateException: Not on FX application thread; currentThread = Timer-2 
at com.sun.javafx.tk.Toolkit.checkFxUserThread(Unknown Source) 
at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(Unknown Source) 
at javafx.scene.Scene.addToDirtyList(Unknown Source) 
at javafx.scene.Node.addToSceneDirtyList(Unknown Source) 
at javafx.scene.Node.impl_markDirty(Unknown Source) 
at javafx.scene.Node$7.invalidated(Unknown Source) 
at javafx.beans.property.BooleanPropertyBase.markInvalid(Unknown Source) 
at javafx.beans.property.BooleanPropertyBase.set(Unknown Source) 
at javafx.css.StyleableBooleanProperty.set(Unknown Source) 
at javafx.scene.Node.setVisible(Unknown Source) 
at Pantallas.PersonalContabilidad$1.run(PersonalContabilidad.java:221) 
at java.util.TimerThread.mainLoop(Unknown Source) 
at java.util.TimerThread.run(Unknown Source) 

correct是一個標籤,我顯示,3秒鐘,它失敗在correct.setVisible(false);這是行221

correct.setVisible(true); 
    timer.schedule(new TimerTask() { 
    @Override 
     public void run() { 
     correct.setVisible(false); 
    } 
    }, 3*1000); 

回答

8

標籤正確的應該從FX應用程序線程進行更新。你應該在這裏使用Platform.runLater()

correct.setVisible(true); 
timer.schedule(new TimerTask() { 
@Override 
    public void run() { 
    Platform.runLater(new Runnable() { 
     public void run() { 
      correct.setVisible(false); 
     } 
    }); 
} 
}, 3*1000); 
0

由於您不是從FX應用程序線程中調用Java FX GUI元素,所以您會收到此異常。您可以從GUI線程訪問GUI元素。您可以使用runLater(Runnable)來確保您的代碼在正確的線程內執行。