对于一个学校项目,我正在为PLC编写一些可视化程序。因此,我有一个包含所有信息的mySQL数据库。当前,当我单击一个按钮时,程序将连接到数据库并在ArrayList中获取它的信息。然后,它检查ArrayList中的信息,并将Data放入ListView中。
问题是,我想让程序在Service中执行该操作。正如我所说的,GUI取决于ArrayList。而且我无法更改服务中的GUI,因为这样会出现此异常
Sep 02, 2016 9:19:02 PM javafx.concurrent.Service lambda$static$488
WARNING: Uncaught throwable in javafx concurrent thread pool
java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-4
at com.sun.javafx.tk.Toolkit.checkFxUserThread(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(Unknown Source)
at javafx.scene.Parent$2.onProposedChange(Unknown Source)
at com.sun.javafx.collections.VetoableListDecorator.setAll(Unknown Source)
at com.sun.javafx.collections.VetoableListDecorator.setAll(Unknown Source)
at com.sun.javafx.scene.control.skin.LabeledSkinBase.updateChildren(Unknown Source)
at com.sun.javafx.scene.control.skin.LabeledSkinBase.handleControlPropertyChanged(Unknown Source)
at com.sun.javafx.scene.control.skin.LabelSkin.handleControlPropertyChanged(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase.lambda$registerChangeListener$61(Unknown Source)
at com.sun.javafx.scene.control.MultiplePropertyChangeListenerHandler$1.changed(Unknown Source)
at javafx.beans.value.WeakChangeListener.changed(Unknown Source)
at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(Unknown Source)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(Unknown Source)
at javafx.beans.property.StringPropertyBase.fireValueChangedEvent(Unknown Source)
at javafx.beans.property.StringPropertyBase.markInvalid(Unknown Source)
at javafx.beans.property.StringPropertyBase.set(Unknown Source)
at javafx.beans.property.StringPropertyBase.set(Unknown Source)
at javafx.beans.property.StringProperty.setValue(Unknown Source)
at javafx.scene.control.Labeled.setText(Unknown Source)
at application.Controller$1$1.call(Controller.java:290)
at application.Controller$1$1.call(Controller.java:1)
at javafx.concurrent.Task$TaskCallable.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at javafx.concurrent.Service.lambda$null$493(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at javafx.concurrent.Service.lambda$executeTask$494(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
我的第一个想法是仅提取服务中的SQL内容,然后通过方法获取数据
service.getValue();
但是问题是我不知道服务何时完成获取数据,因为当前数据为空。
基本上,您想运行一些过程并返回一些值,当然要Service
钉住它。这就是我处理案件的方法,并且效果很好。
Service process = new Service() {
@Override
protected Task createTask() {
return new Task() {
@Override
protected ObjectX call() throws Exception {
updateMessage("Some message that may change with execution");
updateProgress( workDone, totalWork );
return ObjectX;
}
};
}
};
process.setOnSucceeded( e -> {
ObjectX processValue = (ObjectX)process.getValue();
// TODO, . . .
// You can modify any GUI element from here...
// ...with the values you got from the service
});
process.start();
注意事项
protected ObjectX call()
可以返回任何类型的对象。只要确保它是一个对象而不是原始类型即可。您可以在此过程中尽可能填充一些GUI元素,然后将其作为对象返回。protected VBox call()
。。。。return my_vbox;
ObjectX processValue = (ObjectX)processList.getValue();
=>您应该将从Service
背面获得的值转换为要使用的对象。如果仅仅是Object
,您可能不必这样做。但是我怀疑你是否不得不使用just Object
。processList.setOnFailed()
,processList.setOnRunning()
,processList.setOnCancelled()
,processList.setOnScheduled()
,您还可以像这样将某些GUI元素绑定到某些Thread属性
label.textProperty.bind( process.messageProperty ); // messageProperty is a StringProperty
progressBar.progressProperty.bind( process.progressProperty )
确保在调用之前已创建并启动了所有进一步增强过程的方法process.start()
。在开始该过程之前,什么都不会发生。
我希望这有帮助
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句