Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+8 votes
asked in CSC 305 Fall 2024 by (133 points)

2 Answers

+5 votes

I believe when you are instantiating the button to have an action, like when you say button.setOnAction(evt -> method());, you can have it call multiple methods at the same time.

You can do button.setOnAction(evt -> {method1(); method2()});

Another way of doing this is just having the button call one method, but that method calls two separate methods for the actions that you want it to do.

If you want a separate method to be called for when you press it vs when you release it, you just have to set those up separately using onMousePressed() and onMouseReleased()

answered by (1.3k points)
+3 votes

One other way that you can do this as well is with a different method called ".addEventHandler".

You can do the following:

button.addEventHandler(ActionEvent.ACTION, evt -> method1());
button.addEventHandler(ActionEvent.ACTION, evt -> method2());

So that your button has 2 or more action events (there are only two methods used here, but you can definitely put more than two here.

answered by (1.4k points)
...