Upload
others
View
3
Download
0
Embed Size (px)
Citation preview
Chapter 15:
Event-Driven
Programming and
AnimationsDR. JONES
KENNESAW STATE UNIVERSITY
CS 2302
SUMMER 2015
slides created by Rashaad Jones
Overview
Creating Handlers
Registering Handlers
Defining Handlers
Separate class
Inner class
Anonymous
Lambda
Creating Handlers
// creating different handlers
MyHandlerClass myHandlerObj = new MyHandlerClass();
OKHandlerClass okHandler = new OKHandlerClass();
CancelHandlerClass cancelHandler = new CancelHandlerClass();
Registering Handlers
//creating different handlers
MyHandlerClass myHandlerObj = new MyHandlerClass();
OKHandlerClass okHandler = new OKHandlerClass();
CancelHandlerClass cancelHandler = new CancelHandlerClass();
//registering handlers
myBtn.setOnAction(myHandlerObj);
okBtn.setOnAction(okHandler);
cancelBtn.setOnAction(cancelHandler);
Defining Handlers via separate
classes
MyHandlerClass.java
class MyHandlerClass implements EventHandler<ActionEvent>
{
@Override
public void handle(ActionEvent e)
{
//code here will be executed only when event is fired
//example, a button is clicked
System.out.println(“A printout is here only for demonstration purposes”);
}
}
OKHandlerClass.java
class OKHandlerClass implements EventHandler<ActionEvent>
{
@Override
public void handle(ActionEvent e)
{
//code here will be executed only when event is fired
//example, a button is clicked
System.out.println(“OK button is clicked”);
}
}
class CancelHandlerClassimplements EventHandler<ActionEvent>
{
@Override
public void handle(ActionEvent e)
{
//code here will be executed only when event is fired
//example, a button is clicked
System.out.println(“Cancel button is clicked”);
}
}
CancelHandlerClass.java
Defining Handlers via Inner Classes
public class MyProgram extends Application
{
public void Start(Stage pStage)
{
MyHandlerClass myHandlerObj = new
MyHandlerClass();
Button myBtn = new Button();
myBtn.setOnAction(myHandlerObj);
}
class MyHandlerClass implements
EventHandler<ActionEvent>
{
@Override
public void handle(ActionEvent e)
{
System.out.println(“A printout is here only
for demonstration purposes”);
} //end of handle method
} //end of MyHandlerClass inner class
}
Defining Handlers via Anonymous Classes
public class MyProgram extends Application
{
public void Start(Stage pStage)
{
MyHandlerClass myHandlerObj = new
MyHandlerClass();
Button myBtn = new Button();
myBtn.setOnAction( new
EventHandler<ActionEvent>()
{
public void handle(ActionEvent e)
{
System.out.println(“A printout is
here only for demonstration purposes”);
} //end of handle method
}); //ends the setOnAction method
} //end of start
} //end of MyProgram
Defining Handlers via Lambda Expressions
public class MyProgram extends Application
{
public void Start(Stage pStage)
{
MyHandlerClass myHandlerObj = new
MyHandlerClass();
Button myBtn = new Button();
myBtn.setOnAction( e -> {
System.out.println(“A printout is here only for
demonstration purposes”);
}); //ends the setOnAction method
} //end of start
} //end of MyProgram
(HIGHLY RECOMMENDED FOR CS2302)Separate Classes
•Pros: Code is explicit, simple to implement for beginning programmers
•Cons: Required to write more lines of code (writing more lines is preferred for beginners!)
(Also recommended)Inner Classes
•Cons: For complex UIs, code can become unmanageable
(Not recommended for CS2302)Anonymous Classes
•Pros: Less code is written
•Cons: Need one for each handler
(Not recommended for CS2302)Lambda
•Pros: Useful for Android apps programming in functional programming paradigm (we are in OOP)
•Cons: Code is highly unreadable
MouseEvent and KeyEvent
Example