John C
John C

Reputation: 1835

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.

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

Would highly appreciate if anyone can provide any inputs or recommendations.

Upvotes: 2

Views: 2191

Answers (1)

Forager
Forager

Reputation: 317

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. Instead, you can just add an EventHandler to the button to listen for mouse clicks.

Assuming the controller 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
    }

Upvotes: 2

Related Questions