Javafx中的标签绑定异常

时间:2016-01-29 15:24:18

标签: java javafx

我有控制器类:

public void initialize(URL location, ResourceBundle resources) {
    //set Properties' values at the beginning
    setProperties();
    //bind string properties to labels
    word.textProperty().bind(wordString);
    meaning.textProperty().bind(meaningString);
    sentence.textProperty().bind(sentenceString);
    //listener to detect change in index and assign strings accordingly
    DataHelper.intIndexProperty().addListener(
            (v, oldValue, newValue) -> {
                wordString.setValue(DataHelper.getListOfWords().get((int) newValue).get("word"));
                meaningString.setValue(DataHelper.getListOfWords().get((int) newValue).get("meaning"));
                sentenceString.setValue(DataHelper.getListOfWords().get((int) newValue).get("sentence"));
                System.out.print("kjbmmj");
            }
    );
}

我有一个名为TimeChecker的实现runanble的类,它在我的start类的Main方法中实例化并启动。在TimeChecker类中:

public void run() {
    try{
        while (!isDone) {
            System.out.println("Running!!");
            System.out.println();
            DataHelper.setCurrentDate(LocalDate.now());
            if (!DataHelper.getOldDate().equals(DataHelper.getCurrentDate())) {
                DataHelper.setIntIndex(DataHelper.getIntIndex()+1);
                DataHelper.setOldDate(DataHelper.getCurrentDate());
                DataHelper.saveData();
                System.out.println("Saved!");
            }
            Thread.currentThread().sleep(2000);
        }

错误:

Exception in thread "Thread-4" java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-4
at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:204)
at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:438)
at javafx.scene.Parent$2.onProposedChange(Parent.java:364)
at com.sun.javafx.collections.VetoableListDecorator.setAll(VetoableListDecorator.java:113)
at com.sun.javafx.collections.VetoableListDecorator.setAll(VetoableListDecorator.java:108)
at com.sun.javafx.scene.control.skin.LabeledSkinBase.updateChildren(LabeledSkinBase.java:575)
at com.sun.javafx.scene.control.skin.LabeledSkinBase.handleControlPropertyChanged(LabeledSkinBase.java:204)
at com.sun.javafx.scene.control.skin.LabelSkin.handleControlPropertyChanged(LabelSkin.java:49)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase.lambda$registerChangeListener$61(BehaviorSkinBase.java:197)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$$Lambda$160/1022376785.call(Unknown Source)
at com.sun.javafx.scene.control.MultiplePropertyChangeListenerHandler$1.changed(MultiplePropertyChangeListenerHandler.java:55)
at javafx.beans.value.WeakChangeListener.changed(WeakChangeListener.java:89)
at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:182)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
at javafx.beans.property.StringPropertyBase.fireValueChangedEvent(StringPropertyBase.java:103)
at javafx.beans.property.StringPropertyBase.markInvalid(StringPropertyBase.java:110)
at javafx.beans.property.StringPropertyBase.access$000(StringPropertyBase.java:49)
at javafx.beans.property.StringPropertyBase$Listener.invalidated(StringPropertyBase.java:230)
at com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:137)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
at javafx.beans.property.StringPropertyBase.fireValueChangedEvent(StringPropertyBase.java:103)
at javafx.beans.property.StringPropertyBase.markInvalid(StringPropertyBase.java:110)
at javafx.beans.property.StringPropertyBase.set(StringPropertyBase.java:144)
at javafx.beans.property.StringPropertyBase.set(StringPropertyBase.java:49)
at javafx.beans.property.StringProperty.setValue(StringProperty.java:65)
at sample.Controller.lambda$initialize$0(Controller.java:53)
at sample.Controller$$Lambda$86/694065084.changed(Unknown Source)
at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:182)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
at javafx.beans.property.IntegerPropertyBase.fireValueChangedEvent(IntegerPropertyBase.java:106)
at javafx.beans.property.IntegerPropertyBase.markInvalid(IntegerPropertyBase.java:113)
at javafx.beans.property.IntegerPropertyBase.set(IntegerPropertyBase.java:147)
at sample.DataHelper.setIntIndex(DataHelper.java:123)
at sample.TimeChecker.run(TimeChecker.java:26)
at java.lang.Thread.run(Thread.java:745)

intIndexProperty的{​​{1}}启动时,我遇到了这个问题。 我知道这个问题还有其他类似的问题。但我无法正确理解答案;我对我的代码违反java规则的想法含糊不清,除主应用程序线程之外的线程在任何情况下都无法更改UI。任何人都可以正确解释并回答所需的代码,我可以解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

仅在Platform.runLater()内包裹影响GUI的行,这样您就不会让用户界面无响应。

像:

  public void run() {
    try{
        while (!isDone) {
            System.out.println("Running!!");
            System.out.println();
            DataHelper.setCurrentDate(LocalDate.now());
            if (!DataHelper.getOldDate().equals(DataHelper.getCurrentDate())) {
                Platform.runLater(new Runnable(){
                    @Override
                    public void run() {
                        DataHelper.setIntIndex(DataHelper.getIntIndex()+1);
                    }
                });
                DataHelper.setOldDate(DataHelper.getCurrentDate());
                DataHelper.saveData();
                System.out.println("Saved!");
            }