chapter 15: event-driven programming and...
TRANSCRIPT
![Page 1: Chapter 15: Event-Driven Programming and Animationsksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/...Separate Classes (HIGHLY RECOMMENDED FOR CS2302) •Pros: Code is explicit,](https://reader035.vdocuments.mx/reader035/viewer/2022081619/60ea6b52dce0c3064971469f/html5/thumbnails/1.jpg)
Chapter 15:
Event-Driven
Programming and
AnimationsDR. JONES
KENNESAW STATE UNIVERSITY
CS 2302
SUMMER 2015
slides created by Rashaad Jones
![Page 2: Chapter 15: Event-Driven Programming and Animationsksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/...Separate Classes (HIGHLY RECOMMENDED FOR CS2302) •Pros: Code is explicit,](https://reader035.vdocuments.mx/reader035/viewer/2022081619/60ea6b52dce0c3064971469f/html5/thumbnails/2.jpg)
Overview
Creating Handlers
Registering Handlers
Defining Handlers
Separate class
Inner class
Anonymous
Lambda
![Page 3: Chapter 15: Event-Driven Programming and Animationsksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/...Separate Classes (HIGHLY RECOMMENDED FOR CS2302) •Pros: Code is explicit,](https://reader035.vdocuments.mx/reader035/viewer/2022081619/60ea6b52dce0c3064971469f/html5/thumbnails/3.jpg)
Creating Handlers
// creating different handlers
MyHandlerClass myHandlerObj = new MyHandlerClass();
OKHandlerClass okHandler = new OKHandlerClass();
CancelHandlerClass cancelHandler = new CancelHandlerClass();
![Page 4: Chapter 15: Event-Driven Programming and Animationsksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/...Separate Classes (HIGHLY RECOMMENDED FOR CS2302) •Pros: Code is explicit,](https://reader035.vdocuments.mx/reader035/viewer/2022081619/60ea6b52dce0c3064971469f/html5/thumbnails/4.jpg)
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);
![Page 5: Chapter 15: Event-Driven Programming and Animationsksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/...Separate Classes (HIGHLY RECOMMENDED FOR CS2302) •Pros: Code is explicit,](https://reader035.vdocuments.mx/reader035/viewer/2022081619/60ea6b52dce0c3064971469f/html5/thumbnails/5.jpg)
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
![Page 6: Chapter 15: Event-Driven Programming and Animationsksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/...Separate Classes (HIGHLY RECOMMENDED FOR CS2302) •Pros: Code is explicit,](https://reader035.vdocuments.mx/reader035/viewer/2022081619/60ea6b52dce0c3064971469f/html5/thumbnails/6.jpg)
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
}
![Page 7: Chapter 15: Event-Driven Programming and Animationsksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/...Separate Classes (HIGHLY RECOMMENDED FOR CS2302) •Pros: Code is explicit,](https://reader035.vdocuments.mx/reader035/viewer/2022081619/60ea6b52dce0c3064971469f/html5/thumbnails/7.jpg)
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
![Page 8: Chapter 15: Event-Driven Programming and Animationsksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/...Separate Classes (HIGHLY RECOMMENDED FOR CS2302) •Pros: Code is explicit,](https://reader035.vdocuments.mx/reader035/viewer/2022081619/60ea6b52dce0c3064971469f/html5/thumbnails/8.jpg)
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
![Page 9: Chapter 15: Event-Driven Programming and Animationsksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/...Separate Classes (HIGHLY RECOMMENDED FOR CS2302) •Pros: Code is explicit,](https://reader035.vdocuments.mx/reader035/viewer/2022081619/60ea6b52dce0c3064971469f/html5/thumbnails/9.jpg)
(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
![Page 10: Chapter 15: Event-Driven Programming and Animationsksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/...Separate Classes (HIGHLY RECOMMENDED FOR CS2302) •Pros: Code is explicit,](https://reader035.vdocuments.mx/reader035/viewer/2022081619/60ea6b52dce0c3064971469f/html5/thumbnails/10.jpg)
MouseEvent and KeyEvent
Example