As you discovered, the setOnAction(...)
specifies the single event handler code that will be called whenever an action event occurs on the button.
Thus, your second line of code causes the event handler to only run the updateLabel2() code and thus not run the updateLabel1() code anymore.
I think the simplest/best way to handle this is to take the approach suggested by Rory and Mehdi, and just have one event handler that does two things. You could either do this:
btn1.setOnAction(evt -> { updateLabel1(xyz); updateLabel2(abc);} );
Or
btn1.setOnAction(evt -> updateLabels(xyz, abc) );
However, it actually IS possible to add MULTIPLE event handlers to a given
, but you have to use the addEventHandler
method, instead of the simpler .setOnAction
btn1.addEventHandler(ActionEvent.ACTION, evt -> updateLabel1(xyz)));
btn1.addEventHandler(ActionEvent.ACTION, evt -> updateLabel2(abc));
(But I think this approach results in less clean code, and it's not needed in this situation.)