-2

I'm new to JavaFX and I would like to do the following:

Whenever the value of one of my four textfields is changed, I would like a fifth textfield to calculate the sum of the values from the others. Lets assume the values come already as numbers.

Code looks like this:

power1KW = new TextField();
power2KW = new TextField();
... etc.
powerSumKW = new TextField();

How do I do this properly? Do I add a ChangeListener on each of my powerXKW-TextFields? And calculate on change?

Or do I need to add an EventHandler for my powerSumKW-Textfield? How can I handle the Event fired only by the powerXKW-TextFields?

Any advice would be appreciated. Thank you!

Remo
  • 1,112
  • 2
  • 12
  • 25
  • Could you explain what *"doesn't seem to work"* means? – Zephyr May 15 '19 at 03:40
  • I expect the defined EventHandler to work, so whenever I fire the Event, I would like to see a "Hello" in my console. But there's no output. – Remo May 15 '19 at 15:18
  • 1
    I think you're going about this the wrong way. Why do you need a custom event for that? You might want to update your question with a [mcve] that demonstrates what you're trying to do. – Zephyr May 15 '19 at 16:43
  • changed the question completely, it's more generalized now and hopefully more understandable. Any advice would be much appreciated! – Remo May 15 '19 at 20:52

1 Answers1

1

I would just bind the textProperty of the 5th Textfield to the other textfields..something like this:

tf5.textProperty().bind(Bindings.createStringBinding(()->{
           //Do your calculation
           //Return result as String
           return result;
        }, tf1.textProperty(),tf2.textProperty(), tf3.textProperty(), tf4.textProperty()));
micpog90
  • 106
  • 8