gpt4 book ai didi

JavaFX:无法从匿名类调用应用程序的标签字段?

转载 作者:行者123 更新时间:2023-11-29 03:33:02 24 4
gpt4 key购买 nike

我正在组合一个 JavaFX 应用程序,并试图通过将匿名类添加到程序后端的 Listener 表来更改标签的文本。虽然监听器将返回它应该获取的数据,但当标签被匿名类更改时,JavaFX 将崩溃。

前端:

public class App extends Application {

...

final BackendElement backendElement = new BackendElement();

...

@Override
public void start(Stage mainStage) {

...

GridPane stageContents = generateGridPane(mainStage);

...

mainStage.setScene(new Scene(stageContents));
mainStage.show();
}


private GridPane getGridPane(final Stage mainStage) {

...

final Label labelToBeUpdated = new Label("");

...

backendElement.addModelChangedListener(new ModelChangedListener() {
@Override
public void modelChanged(ModelChangedEvent ev) {
labelToBeUpdated.setText(ev.getSource()); //<- Causes verbose JFX Exception
}
});
}

ModelChangedListener 和 ModelChangedEvent 只是定制观察者模式的一部分。该结构确实有效,因为用简单的代码替换了错误生产线

System.out.println(ev.getSource);

会打印出想要的数据。

我认为私有(private)类的标签不能从匿名类中调用。事实证明,App 类中的 final 字段也不能。有没有办法在不丢失观察者模式的情况下解决这个问题?

这是完整的堆栈跟踪:

Exception in thread "Timer-0" java.lang.IllegalStateException: Not on FX application thread; currentThread = Timer-0
at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:237)
at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:397)
at javafx.scene.Parent$1.onProposedChange(Parent.java:245)
at com.sun.javafx.collections.VetoableObservableList.setAll(VetoableObservableList.java:90)
at com.sun.javafx.collections.ObservableListWrapper.setAll(ObservableListWrapper.java:314)
at com.sun.javafx.scene.control.skin.LabeledSkinBase.updateChildren(LabeledSkinBase.java:602)
at com.sun.javafx.scene.control.skin.LabeledSkinBase.handleControlPropertyChanged(LabeledSkinBase.java:209)
at com.sun.javafx.scene.control.skin.SkinBase$3.changed(SkinBase.java:282)
at javafx.beans.value.WeakChangeListener.changed(WeakChangeListener.java:107)
at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:196)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:100)
at javafx.beans.property.StringPropertyBase.fireValueChangedEvent(StringPropertyBase.java:121)
at javafx.beans.property.StringPropertyBase.markInvalid(StringPropertyBase.java:128)
at javafx.beans.property.StringPropertyBase.set(StringPropertyBase.java:161)
at javafx.beans.property.StringPropertyBase.set(StringPropertyBase.java:67)
at javafx.beans.property.StringProperty.setValue(StringProperty.java:84)
at javafx.scene.control.Labeled.setText(Labeled.java:135)
at ui.App$12.modelChanged(App.java:349)
at ui.BackendElement$CustomTimerTask.informAllListeners(BackendElement.java:108)
at ui.BackendElement$CustomTimerTask.access$0(BackendElement.java:105)
at ui.BackendElement$CustomTimerTask$1.run(BackendElement.java:87)
at java.util.TimerThread.mainLoop(Unknown Source)
at java.util.TimerThread.run(Unknown Source)

最佳答案

解决方法:使用 Platform.runLater() 包装您希望听众执行的任何操作。

        @Override
public void modelChanged(ModelChangedEvent ev) {

final ModelChangedEvent event = ev;

Platform.runLater(new Runnable(){
@Override
public void run() {
labelToBeChanged.setText((String)event.getSource());
}
});
}

关于JavaFX:无法从匿名类调用应用程序的标签字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17075447/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com