从线程更新JavaFX场景图 [英] Update JavaFX scene graph from a Thread

查看:150
本文介绍了从线程更新JavaFX场景图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据客户端输入更新我的GUI。从后台任务中调用我的控制器类方法。但它无法更新GUI,因为它不是JavaFX应用程序线程..请帮助。

I need to update my GUI based on client input. Calling my controller class method, from the background task works. But it can't update the GUI, because it is not the JavaFX application thread..please help.

我尝试了很多相关的 Q&一个,但我仍然感到困惑。

I tried many of the related Q & A, but I am still confused.

我应该使用平台。 runLater 任务

这是我的班级,我创建一个控制器的实例 class

Here's my class where I create an instance of controller class

public class FactoryClass {

    public static Controller_Gui1 createGUI() {
        FXMLLoader fxLoader = new FXMLLoader();
        fxLoader.setLocation(MainApp_Gui1.class.getResource("/com/Gui_1.fxml"));
        AnchorPane anchorPane = null;
        try {
            anchorPane = (AnchorPane) fxLoader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Controller_Gui1 controller_Gui1 = (Controller_Gui1) fxLoader
                .getController();
        Scene scene = new Scene(anchorPane);
        //System.out.println(scene);
        controller_Gui1.setScene(scene);
        return controller_Gui1;
    }
}

控制器类

@FXML
Button B1 = new Button();
@FXML
public void handleButton1() {
    B1.setDisable(true);
}

申请类

public class MainApp_Gui1 extends Application {
    Controller_Gui1 cGui;

    @Override
    public void start(Stage primaryStage) throws Exception {
        initScene(primaryStage);
        primaryStage.show();
        System.out.println("asdasd");
        SceneSetting sceneSetting = new SceneSetting();
        //handleEvent();
        System.out.println("after");
        sceneSetting.setSceneAfter();
        System.out.println("after2");

    }

    // creating scene
    private void initScene(Stage primaryStage) throws IOException {
        primaryStage.setScene(getScene(primaryStage));

    }

    public Scene getScene(Stage primaryStage) {
        Controller_Gui1 cGui;
        cGui = FactoryClass.createGUI();
        return cGui.getScene();
    }

    public void ExcessFromOutside() {
        Platform.runLater(new Runnable() {
            public void run() {
               System.out.println(Platform.isFxApplicationThread());
               cGui.handleButton1();
            }
        });

    }

    public static void main(String[] args) {
        launch(args);
    }
}

我想拨打 ExcessFromOutside ()来自另一个线程的方法。

I want to call ExcessFromOutside() method from another thread.

尝试更新GUI时我得到一个空指针异常
这是我的应用程序类

I got a null pointer exception while trying to update the GUI Here's my application class

public class MainAppGui1 extends Application {
Controller_Gui1 controller_Gui1;
@Override
public void start(Stage primaryStage) throws Exception {
    initScene(primaryStage);
    primaryStage.show();

}
// creating scene
public void initScene(Stage primaryStage) throws IOException {
    FXMLLoader fxLoader = new FXMLLoader();
    fxLoader.setLocation(MainApp_Gui1.class.getResource("/com/Gui_1.fxml"));
    AnchorPane anchorPane=new AnchorPane();
    anchorPane = (AnchorPane) fxLoader.load();
    Controller_Gui1 controller_Gui1 = (Controller_Gui1) fxLoader.getController();
    Scene scene = new Scene(anchorPane);
    primaryStage.setScene(scene);
}
@FXML
public  void ExcessFromOutside()
{
    Platform.runLater(new Runnable() {      
        @Override 
        public void run() {

            System.out.println("called atleast");
            controller_Gui1.handleButton1();

        }
    });
}
public static void main(String[] args) {
    launch(args);
}


}

这是我试图更新GUI的类

and this is the class from where i tried to update the GUI

public class Hudai {
public static void main(String args[]) throws InterruptedException
{
    new Thread()
    {
        public void run()
        {
            MainAppGui1.main(null);
        }
    }.start();
    Thread.sleep(5000);
    MainAppGui1 m = new MainAppGui1();
    m.ExcessFromOutside();      
}

}


推荐答案

要在另一个线程中禁用按钮,您可以使用任务的 updateValue

To disable your button in a different thread you can use Task's updateValue.

Task<Boolean> task = new Task<Boolean>() {
    @Override
    public Boolean call() {
         ... // The task that this thread needs to do
         updateValue(true);
         ...
         return null;
    }
};
button.disableProperty().bind(task.valueProperty());

如果你想用一个新的线程调用一个改变场景图的方法,最好的你有机会在其中使用 Platform.runLater()

If you want to use a new thread to call a method, which alters the scene graph, the best chance you have is to use Platform.runLater() in it.

//code inside Thread
...
// code to run on the JavaFX Application thread
Platform.runLater(new Runnable() {
    @Override 
    public void run() {
         handleButton1();
    }
});
...

这篇关于从线程更新JavaFX场景图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆