-3

I speak Spanish not English, I am using the google translator.

I have a .FXML form with many TextField and a button to click and perform a mathematical operation with each of the values entered in TextField. I created a method that obtains the value of the text fields and performs the mathematical operation.

But if any of the text fields has changed the value, it must be recalculated by clicking on the button. I need that when pressing the enter key in any TextField, the method of mathematical operation is automatically executed, but I do not want to use the keyPressed method in each text field, it would be a long code, invoke the keyPressed method for each TextField.

I was thinking about creating a listener or a kind of Binding, that I join all the text fields or that I detected when there were changes in the text fields, or when the value entered is greater than zero.

textField.textProperty().addListener...

that method works, but I have many TextField, and I have to repeat that code many times. And I do not want to do that.

txt1..textProperty().addListener...
txt2..textProperty().addListener...
txt3..textProperty().addListener...
txt4..textProperty().addListener...
txt5..textProperty().addListener...
...
...

BooleanBinding bd = Binding.(txt1.textProp..., txt2textProp..., txt3.... ???

will it be possible?

enter image description here

  • Welcome to SO! You really need to clarify your question more and put in some effort to help us help you. Please take the [tour] and read the [ask] article for help. – Zephyr Jan 14 '19 at 13:01
  • Possible duplicate of [Value Change Listener for JavaFX's TextField](https://stackoverflow.com/questions/30160899/value-change-listener-for-javafxs-textfield) – Hulk Jan 14 '19 at 13:07
  • @Hulk Thanks, but no work me. I edited the Question. Please, read it again. – Nelson Castiblanco Jan 14 '19 at 13:21
  • @Zephyr I edited the Question. Please, read it again – Nelson Castiblanco Jan 14 '19 at 13:21
  • You can create one listener and reuse it on all your `TextFields` – Zephyr Jan 14 '19 at 15:16
  • Or, why not use the `setOnAction()` method of your `TextField` to call the calculate method? `textField.setOnAction(e -> calculate());` – Zephyr Jan 14 '19 at 15:23
  • @Zephyr But I would have to use the setOnAction method for all 30 TextFields and it is repeated many times ... I want to know if there is a way to call the method calculate () when all TextFields have a value greater than or equal to zero. – Nelson Castiblanco Jan 14 '19 at 18:57
  • Your question say 5 textfields. But no, there is no way to make all textfields behave a certain way without telling all textfields to behave a certain way... – Zephyr Jan 14 '19 at 19:13
  • @Zephyr you are reason – Nelson Castiblanco Jan 15 '19 at 15:42

1 Answers1

0
EventHandler<KeyEvent> teclaPresionada = (event) -> {
// Colocar todo el codigo aca
};

campo1.addEventHandler(KeyEvent.KEY_PRESSED, teclaPresionada);
campo2.addEventHandler(KeyEvent.KEY_PRESSED, teclaPresionada);
campo3.addEventHandler(KeyEvent.KEY_PRESSED, teclaPresionada);
campo4.addEventHandler(KeyEvent.KEY_PRESSED, teclaPresionada);  
campo5.addEventHandler(KeyEvent.KEY_PRESSED, teclaPresionada);