1

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>
4
  • may you post some snippet? onAction isn't fired by double-click but any action. Commented Jun 1, 2017 at 8:51
  • I posted the function, which is activated only with double click. Its part of the gridpane of the fxml. Commented Jun 1, 2017 at 8:57
  • Not sure what exactly should I poste, since the code inside the Button1Action does not contain any event handler. Commented Jun 1, 2017 at 8:59
  • Create a minimal reproducible example and post it in the question. It's not possible to diagnose the problem from these snippets. Commented Jun 1, 2017 at 13:33

1 Answer 1

1

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");
}
answered Jun 1, 2017 at 10:20
Sign up to request clarification or add additional context in comments.

2 Comments

My code is as your second example. This is the strange thing. I dont have setOnAction inside the code.
In this case please update your question with an example.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.