Wednesday, 18 November 2020

JavaFX: Event Handling using method reference

 

Step 1: Define a method that handles event.

private void buttonHandler(ActionEvent event) {

         System.out.println("button clicked");

}

 

Step 2: Set the method ‘buttonHandler’ using method reference on action event.

button1.setOnAction(this::buttonHandler);

 

Find the below working application.

 

EventHandlingByMethodReference.java

package com.sample.app.eventhandling;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.effect.DropShadow;
import javafx.scene.layout.FlowPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.stage.Stage;

public class EventHandlingByMethodReference extends Application {

	public static void main(String args[]) {
		launch(args);
	}

	private void buttonHandler(ActionEvent event) {
		System.out.println("button clicked");
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		DropShadow dropShadow = new DropShadow();
		dropShadow.setOffsetX(5);
		dropShadow.setOffsetY(5);
		dropShadow.setColor(Color.GRAY);

		Button button1 = new Button("Click Me");
		button1.setTextFill(Color.BLACK);
		button1.setFont(Font.font("Verdana", FontPosture.REGULAR, 35));

		button1.setOnAction(this::buttonHandler);

		FlowPane.setMargin(button1, new Insets(50, 50, 50, 50));

		FlowPane flowPane = new FlowPane();
		flowPane.setOrientation(Orientation.HORIZONTAL);
		flowPane.setEffect(dropShadow);
		flowPane.getChildren().add(button1);

		Scene scene = new Scene(flowPane, 500, 400, Color.LIGHTYELLOW);

		primaryStage.setTitle("Event Handling Demo");
		primaryStage.setScene(scene);
		primaryStage.show();
	}
}

 

Output

 

 



Whenever you click on the button ‘Click Me’, you will see below message in the console.

 

button clicked

Previous                                                    Next                                                    Home

No comments:

Post a Comment