23
Dr Walid M. Aly CS244 Advanced programming Applications Lecture 8 Event-Driven Programming

Lec#8 JavaFX Events

Embed Size (px)

Citation preview

Dr Walid M. Aly

CS244 Advanced programming Applications

Lecture8

Event-Driven Programming

2

Procedural vs. Event-Driven Programming

§ Procedural programming is executed in procedural order.

§ In event-driven programming, code is executed upon activation of events.

33

Handling GUI Events

Source object (e.g., button)Listener object contains a method for

processing the event.

4

Events

qAn event can be defined as a type of signalto the program that something hashappened.

qThe event is generated by external useractions such as mouse movements, mouseclicks, or keystrokes.

5

Event Classes

6

Event Information

§ An event object contains whatever properties arepertinent to the event.

§ You can identify the source object of the eventusing the getSource( ) instance method in theEventObject class.

§ The subclasses of EventObject deal with specialtypes of events, such as button actions, windowevents, component events, mouse movements, andkeystrokes.

7

The Delegation Model

8

The Delegation Model

9

Selected User Actions and Handlers

Hint: For KeyEvent , the Node must have input focus when event occursAnd can request focus using method requestFocus()

10

The Delegation Model: Example

Button btOK = new Button("OK");

OKHandlerClass handler = new OKHandlerClass();

btOK.setOnAction(handler);

Types of Listeners implementation

1. The Listener class can be the same class as the GUI class

2. Using Lambda Expression 3. The Listener an be an external class4. The listener can be an inner class , this inner

class can be anonymous

11

12

Example 1•The example displays a button in the frame.•A message is displayed on the console when a button is clicked.

GUIHandler

GUIHandlerWLambda.java

Example 1 Using same GUI class as Listener

13

public class GUIHandler extends Application implements EventHandler<ActionEvent>{

public void start(Stage primaryStage) throws Exception{primaryStage.setTitle("Hello World");Button button=new Button("Click me");button.setOnAction(this);

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

launch(args); }

public void handle(ActionEvent event) {System.out.println(”Bttn clicked"); }

}

Example 1 Using Outer class as Listener

14GUIHandler2.java

public class GUIHandler2 extends Application {public void start(Stage primaryStage){…….

Button button=new Button("Click me");Handler handler1 =new Handler();button.setOnAction(handler1);

…..}

class Handler implements EventHandler<ActionEvent>public void handle(ActionEvent event) {

System.out.println("mouse clicked");}

}

Example 1 Using Inner class as Listener

15

public class GUIHandler2 extends Application {class Handler implements EventHandler<ActionEvent>

public void handle(ActionEvent event) {System.out.println("mouse clicked");

}}

public void start(Stage primaryStage){…….

Button button=new Button("Click me");Handler handler1 =new Handler();button.setOnAction(handler1);}

…..}

Example 1 Using Anonymous Inner class as Listener

16

public class GUIHandler2 extends Application {

public void start(Stage primaryStage){…….

Button button=new Button("Click me");button.setOnAction(new EventHandler<ActionEvent>() {public void handle(ActionEvent e) {

System.out.println("mouse clicked");}

});…..}

Example1 Using Lambda Expression

17

public class GUIHandlerWLambda extends Application {

@Overridepublic void start(Stage primaryStage) throws Exception{

primaryStage.setTitle("Hello World");Button button=new Button("Click me");button.setOnAction(e->System.out.println("mouse clicked"));…………………….

}

GUIHandlerWLambda.java

Example2: Detecting source of event

18

DetectingSourceDemo.java

19

Import……………….public class DetectingSourceDemo extends Application {

public void start(Stage primaryStage) throws Exception{Button button1=new Button("button1");

Button button2=new Button("button2");Button button3=new Button("button3");TextField textField=new TextField();…………Alistener aListener=new Alistener(textField);button1.setOnAction(aListener);button2.setOnAction(aListener);button3.setOnAction(aListener);

……………………………………………………...}

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

class Alistener implements EventHandler<ActionEvent>{

TextField textField;

public Alistener(TextField textField){this.textField=textField;

}public void handle(ActionEvent event) {

Button button=(Button) event.getSource();textField.setText(button.getText() + "

clicked");}

}

Example 2 Using External class

20

MouseEvent

Example: Mouse Event Demo

21

public void start(Stage primaryStage) {// Create a pane and set its propertiesPane pane = new Pane();Text text = new Text(20, 20, "Programming is fun");pane.getChildren().addAll(text);text.setOnMouseDragged(e -> {text.setX(e.getX());text.setY(e.getY());

});

MouseEventDemo

22

The KeyEvent Class

Example: Key Event Demo

23KeyEventDemo.java

pane.getChildren().add(text);text.setOnKeyPressed(e -> {switch (e.getCode()) {case DOWN: text.setY(text.getY() + 10); break;case UP: text.setY(text.getY() - 10); break;case LEFT: text.setX(text.getX() - 10); break;case RIGHT: text.setX(text.getX() + 10); break;default:if (Character.isLetterOrDigit(e.getText().charAt(0)))text.setText(e.getText());

}});