Skip to content

Commit e1483b6

Browse files
committed
Added Event handler with Inner class and lambda expression
1 parent 1520e2b commit e1483b6

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.kazoo.innerLambdas;
2+
3+
import javafx.application.Application;
4+
import javafx.event.ActionEvent;
5+
import javafx.event.EventHandler;
6+
import javafx.scene.Scene;
7+
import javafx.scene.control.Button;
8+
import javafx.scene.layout.StackPane;
9+
import javafx.stage.Stage;
10+
11+
public class InnerLambdas extends Application{
12+
Button btn;
13+
14+
public static void main(String[] args) {
15+
launch(args);
16+
17+
}
18+
19+
public void start(Stage primaryStage)throws Exception{
20+
primaryStage.setTitle("User Events");
21+
22+
btn = new Button();
23+
btn.setText("Click Me");
24+
//using and anonymous inner class to handle events
25+
btn.setOnAction(new EventHandler<ActionEvent>(){
26+
public void handle(ActionEvent event) {
27+
System.out.println("I'm an Inner class");
28+
}
29+
});
30+
31+
32+
33+
StackPane layout = new StackPane();
34+
layout.getChildren().add(btn);
35+
36+
Scene scene = new Scene(layout, 350, 350);
37+
38+
primaryStage.setScene(scene);
39+
primaryStage.show();
40+
41+
}
42+
43+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import javafx.application.Application;
2+
import javafx.scene.Scene;
3+
import javafx.scene.control.Button;
4+
import javafx.scene.layout.StackPane;
5+
import javafx.stage.Stage;
6+
7+
public class Main extends Application{
8+
Button btn;
9+
public static void main(String[] args) {
10+
launch(args);
11+
12+
}
13+
14+
@Override
15+
public void start(Stage primaryStage) throws Exception {
16+
primaryStage.setTitle("Lambda Expression");
17+
18+
btn = new Button();
19+
btn.setText("Click Here");
20+
btn.setOnAction(e -> System.out.println("From Lambda Expression"));
21+
22+
StackPane layout = new StackPane();
23+
layout.getChildren().add(btn);
24+
25+
Scene scene = new Scene(layout, 300, 200);
26+
27+
primaryStage.setScene(scene);
28+
primaryStage.show();
29+
30+
31+
32+
33+
}
34+
35+
}

0 commit comments

Comments
 (0)