简体   繁体   English

单击按钮时更新GUI单击:JavaFX

[英]Update GUI upon button Click: JavaFX

Looking to update GUI first thing upon click of a button however Platform.runLater executes at a later stage and am looking for the piece of code which updates the GUI to happen first thing upon click of a button. 希望在单击按钮时首先更新GUI,但是Platform.runLater在稍后的阶段执行,并且正在寻找使GUI发生更新的代码片段。

Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        //Update GUI here
                    }
                });

Would highly appreciate if anyone can provide any inputs or recommendations. 如果有人可以提供任何意见或建议,我们将不胜感激。

Although the API specifies that Platform.runLater "runs the specified Runnable on the JavaFX Application Thread at some unspecified time in the future", it usually takes little to no time for the specified thread to be executed. 尽管API指定Platform.runLater “在将来某个未指定的时间在JavaFX Application线程上运行指定的Runnable”,但是执行指定线程通常花费很少甚至没有时间。 Instead, you can just add an EventHandler to the button to listen for mouse clicks. 相反,您可以仅向按钮添加一个EventHandler来监听鼠标单击。

Assuming the controller implements Initializable 假设控制器implements Initializable

    @FXML Button button;

    @Override
    public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
        button.setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                updateGUI();
            }
        });
    }

    private void updateGUI() {
        // code
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM