konstantin
konstantin

Reputation: 893

Javafx button event needs double clicking

I have a javafx scene with several buttons within. The only way that the events of the button will be activated is by double cliking. In fxml the button is using the following action method onAction="#Button1Action". How can I change the functionality of the button1Action event from double click to just one click?

The function onAction:

 @FXML
 private void Button1Action(ActionEvent event) {
 }

and the fxml code:

<Button id="button1" fx:id="button1" maxHeight="1.79.." maxWidth="1.79.." mnemonicParsing="false" onAction="#Button1Action" text="Answer" GridPane.columnIndex="3" GridPane.columnSpan="3" GridPane.halignment="CENTER" GridPane.rowIndex="7" GridPane.valignment="CENTER">
        <GridPane.margin>
            <Insets bottom="30.0" left="30.0" right="30.0" top="30.0" />
        </GridPane.margin>
</Button>

Upvotes: 1

Views: 1764

Answers (1)

DVarga
DVarga

Reputation: 21799

You did not posted the body of Button1Action, but most probably it is looking like:

@FXML
private void Button1Action(ActionEvent event) {
    button1.setOnAction(e -> System.out.println("Button clicked"));
}

What happens here, that you assign the listener inside the listener, so the actual listener body will be executed on the second click.

Trivial fix is:

@FXML
private void Button1Action(ActionEvent event) {
    System.out.println("Button clicked");
}

Upvotes: 1

Related Questions