5-Event-Driven Programming
5-Event-Driven Programming
PROGRAMMING
Event-Driven Programming
MUSBAH J. MOUSA
IYAS A. I. ESHAIKHKHALIL
1
OUTLINES
• Procedural vs. Event-Driven Programming.
• Events:
• Sources
• Classes
• Information.
• Handler classes.
• Register handler objects.
• Defining Handler Classes using:
• Inner classes
• Anonymous classes
• Lambda expressions.
• MouseEvents
• KeyEvents
• Observable Objects Listeners.
2
PROCEDURAL VS. EVENT-DRIVEN
PROGRAMMING
3
PROCEDURAL PROGRAMMING
Code
String name = "Java";
String welcomeMessage = String.format("Hello %s!", name);
System.out.println(welcomeMessage);
Output
4
PROCEDURAL PROGRAMMING
Code
String name = "Java"; Executed
String welcomeMessage = String.format("Hello %s!", name);
System.out.println(welcomeMessage);
Output
5
PROCEDURAL PROGRAMMING
Code
String name = "Java"; Executed
String welcomeMessage = String.format("Hello %s!", name); Executed
System.out.println(welcomeMessage);
Output
6
PROCEDURAL PROGRAMMING
Code
String name = "Java"; Executed
String welcomeMessage = String.format("Hello %s!", name); Executed
System.out.println(welcomeMessage); Executed
Output
Hello Java!
7
EVENT-DRIVEN PROGRAMMING
Code
String name = "Java";
String welcomeMessage = String.format("Hello %s!", name);
Button btPrint = new Button(“Print”);
btPrint.setOnAction(e -> {System.out.println(welcomeMessage);}
Output
8
EVENT-DRIVEN PROGRAMMING
Code
String name = "Java"; Executed
String welcomeMessage = String.format("Hello %s!", name);
Button btPrint = new Button(“Print”);
btPrint.setOnAction(e -> {System.out.println(welcomeMessage);}
Output
9
EVENT-DRIVEN PROGRAMMING
Code
String name = "Java"; Executed
String welcomeMessage = String.format("Hello %s!", name); Executed
Button btPrint = new Button(“Print”);
btPrint.setOnAction(e -> {System.out.println(welcomeMessage);}
Output
10
EVENT-DRIVEN PROGRAMMING
Code
String name = "Java"; Executed
String welcomeMessage = String.format("Hello %s!", name); Executed
Button btPrint = new Button(“Print”); Executed
btPrint.setOnAction(e -> {System.out.println(welcomeMessage);}
Output
11
EVENT-DRIVEN PROGRAMMING
Code
String name = "Java"; Executed
String welcomeMessage = String.format("Hello %s!", name); Executed
Button btPrint = new Button(“Print”); Executed
btPrint.setOnAction(e -> {System.out.println(welcomeMessage);} Executed
Nothing
printed!
Output
12
EVENT-DRIVEN PROGRAMMING
Code
String name = "Java"; Executed
String welcomeMessage = String.format("Hello %s!", name); Executed
Button btPrint = new Button(“Print”); Executed
btPrint.setOnAction(e -> {System.out.println(welcomeMessage);} Executed
Output
Hello Java!
Click!
13
HANDLING GUI EVENTS
14
EVENTS
• An event can be defined as a type of signal to the program
that something has happened.
• The event is generated by external user actions such as
mouse movements, mouse clicks, or keystrokes.
15
EVENT CLASSES
16
EVENT INFORMATION
• An event object contains whatever properties are pertinent
to the event.
• You can identify the source object of the event using the
getSource() instance method in the EventObject class.
• The subclasses of EventObject deal with special types of
events, such as button actions, window events, mouse
movements, and keystrokes.
17
SELECTED USER ACTIONS
AND HANDLERS
18
THE DELEGATION MODEL
• Delegation is a model where an object forwards a specific
task or responsibility to another object without itself
performing the task.
• In other words, delegation involves passing on the
responsibility of a certain task to another object, which is
better equipped to handle it.
19
THE DELEGATION MODEL
20
THE DELEGATION MODEL:
EXAMPLE
Example
Button btOK = new Button("OK");
OKHandlerClass handler = new OKHandlerClass();
btOK.setOnAction(handler);
21
INNER CLASS LISTENERS
• A listener class is designed specifically to create a listener
object for a GUI component (e.g., a button).
• It will not be shared by other applications.
• So, it is appropriate to define the listener class inside the
frame class as an inner class.
22
INNER CLASSES
• Inner class: A class is a member of another class.
• Advantages: In some applications, you can use an inner
class to make programs simple.
• An inner class can reference the data and methods defined
in the outer class in which it nests, so you do not need to
pass the reference of the outer class to the constructor of
the inner class.
23
INNER CLASSES
• An inner class can be declared public, protected, or private
subject to the same visibility rules applied to a member of
the class.
• An inner class can be declared static. A static inner class
can be accessed using the outer class name. A static inner
class cannot access non-static members of the outer class
24
INNER CLASSES
25
ANONYMOUS INNER CLASSES
• Inner class listeners can be shortened using anonymous
inner classes.
• An anonymous inner class is an inner class without a
name.
• It combines declaring an inner class and creating an
instance of the class in one step.
• It's a way to create a one-time use class that can be used to
define a behavior that is only needed in a specific context
or for a limited time.
26
ANONYMOUS INNER CLASSES
• An anonymous inner class is declared as follows:
new SuperClassName/InterfaceName() {
// Implement or override methods in superclass or interface
// Other methods if necessary
}
27
ANONYMOUS INNER CLASSES
• An anonymous inner class must always extend a
superclass or implement an interface, but it cannot have an
explicit extends or implements clause.
• An anonymous inner class must implement all the abstract
methods in the superclass or in the interface.
28
ANONYMOUS INNER CLASSES
29
SIMPLIFYING EVENT HANDING
USING LAMBDA EXPRESSIONS
• Lambda expression is a new feature in Java 8.
• Lambda expressions can be viewed as an anonymous
method with a concise syntax.
• For example, the following code in (a) can be greatly
simplified using a lambda expression in (b) in three lines.
(a) Anonymous inner class event handler (b) Lambda expression event handler
30
BASIC SYNTAX FOR A
LAMBDA EXPRESSION
• The basic syntax for a lambda expression is either
31
SINGLE ABSTRACT METHOD
INTERFACE (SAM)
• The statements in the lambda expression is all for that
method.
• If it contains multiple methods, the compiler will not be
able to compile the lambda expression.
• So, for the compiler to understand lambda expressions, the
interface must contain exactly one abstract method.
• Such an interface is known as a functional interface, or a
Single Abstract Method (SAM) interface.
32
THE MouseEvent CLASS
33
THE MouseEvent CLASS
• Four constants are defined in MouseButton:
• PRIMARY => left button
• SECONDARY => right button
• MIDDLE => middle button
• NONE => none button
• You can use the getButton() method to detect which button is
pressed.
• For example, getButton() == MouseButton.SECONDARY tests if the
right button was pressed.
• You can also use the following methods:
• isPrimaryButtonDown()
• isSecondaryButtonDown()
• isMiddleButtonDown()
34
THE MouseEvent CLASS
35
KeyEvent
• The key pressed handler is invoked when a key is pressed.
• The key released handler is invoked when a key is released.
• The key typed handler is invoked when a Unicode character
is entered.
• If a key does not have a Unicode (e.g., function keys,
modifier keys, action keys, arrow keys, and control keys),
the key typed handler will not be invoked.
36
KeyEvent
• Every key event has an associated code that is returned by
the getCode() method in KeyEvent.
• For the key-pressed and key-released events:
• getCode() returns the value as defined in the table.
• getText() returns a string that describes the key code.
• getCharacter() returns an empty string.
• For the key-typed event:
• getCode() returns UNDEFINED.
• getCharacter() returns the Unicode character or a sequence of
characters associated with the key-typed event.
37
THE KeyCode CONSTANTS
38
FOCUS
• Only a focused node can receive KeyEvent .
• Invoking requestFocus() on text enables text to receive key
input.
• This method must be invoked after the stage is displayed.
39
LISTENERS FOR OBSERVABLE
OBJECTS
• You can add a listener to process a value change in an
observable object.
• An instance of Observable is known as an observable
object, which contains the addListener(InvalidationListener
listener) method for adding a listener.
• Once the value is changed in the property, a listener is
notified.
• The listener class should implement the InvalidationListener
interface, which uses the invalidated(Observable o) method
to handle the property value change.
• Every binding property is an instance of Observable.
40
ANY QUESTIONS?
41