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

+9 votes

How do I add event handlers? Specifically buttons

asked in CSC305 Fall 2023 by (1 point)

2 Answers

+4 votes
 
Best answer

The lab includes this line of code:

btnD4.setOnMouseClicked(evt -> rollDie(4))

OnMouseClicked is an event, and we want to associate a "handler" for it -- i.e. some code that runs whenever the mouse gets clicked within the btnD4 UI widget.

To understand this deeper, Java is technically passing in a new object of a class that implements the EventHandler interface, and includes a method that looks like this:

    btnD4.setOnMouseClicked(new EventHandler<MouseEvent>() {

		@Override
		public void handle(MouseEvent evt) {
			rollDie(4);
			
		}
    });

However, this all gets hidden within the arrow "lambda expression"

(evt -> rollDie(4))
answered by (508 points)
selected by
+4 votes
.setOnMouseClicked();
answered by (1 point)
...