JavaFX:服务和 GUI

Tor*_*ben 5 java multithreading javafx java-8

对于学校项目,我正在为 PLC 编写一些可视化程序。因此,我有一个包含所有信息的 mySQL 数据库。目前,当我单击按钮时,程序会连接到数据库并在 ArrayList 中获取它的信息。然后它检查ArrayList 中的信息并将Data 放入ListView。

问题是,我想让程序在服务中做到这一点。正如我所说,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)
Run Code Online (Sandbox Code Playgroud)

我的第一个想法是只提取服务中的 SQL 内容,然后通过方法获取数据

service.getValue();
Run Code Online (Sandbox Code Playgroud)

但问题是我不知道服务何时完成获取数据,因为目前数据为空。

Oni*_*iel 6

基本上,你想运行一些进程并返回一些值,当然要解决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();
Run Code Online (Sandbox Code Playgroud)

注意事项

  1. 内部方法protected ObjectX call()可以返回任何类型的对象。只要确保它是一个对象而不是原始类型。您可以在此过程中填充一些 GUI 元素并将其作为对象返回,例如。protected VBox call() . . . .return my_vbox;
  2. ObjectX processValue = (ObjectX)processList.getValue();=> 您应该将从Service背面获得的值转换为要使用的对象。如果只是Object,您可能不必。但我怀疑您是否必须使用 just Object
  3. 另见processList.setOnFailed(), processList.setOnRunning(), processList.setOnCancelled(), processList.setOnScheduled(),
  4. 您还可以像这样将一些 GUI 元素绑定到一些 Thread 属性

    label.textProperty.bind( process.messageProperty ); // messageProperty is a StringProperty
    progressBar.progressProperty.bind( process.progressProperty )
    
    Run Code Online (Sandbox Code Playgroud)
  5. 在调用之前,请确保已创建并启动了所有进一步增强流程的方法process.start();在您开始该过程之前,什么都不会发生。

我希望这有帮助