The JavaFX framework provides more than one way to handle events. For event hand
ID: 3904203 • Letter: T
Question
The JavaFX framework provides more than one way to handle events. For event handlers, we could use inner classes, anonymous inner classes, or the new Java 8 feature of lambda expressions.
Unzip the attached NetBeans project zip file (U4D1_HandleEvents.zip) and load it into your NetBeans IDE.
The project uses an inner class to handle the click event on the button node. Rewrite the event handler using either an anonymous inner class, or a lambda expression.
This is the provided code:
U4D1 HandleEvents.java x Source History l@ ?.?.15 E -blip?%??0 ?|? ackage uidl handleevents 3 import Javatx.application.Application; 4import Javafx.event.ActionEvent: 5mport javafx.event.EventHandler: 6 import javafx.scene. Scene: 7import javafx.scene.control.Button; 8import javafx.scene.layout.StackPane; 91mport Javafx.stage.Stage: 10 11ublic class U4D1 HandleEvents extends Application 12 Override public void start (Stage primaryStage) t Button btn new Button ) ben.setText ("Say 'Hello World') 16 18 19 20 21 //using inner class btn.setOnAction (new MyEventHandler StackPane rootnew StackPane (0 root.getchildren add (btn) 23 24 25 26 27 28 29 30 31 Scene scene-new Scene (root, 300, 250): prinaryStage.setTitle ("Hello World!") primarystage.setscene (scene) primarystage.show ) clas MyEventHandler implements EventHandler { public void handle (ActionEvent event) System.out.println("Hello Worid!") 34 35 36 37 38 39 40 41 42 43 eparam args the command line arguments public static void main (String [ args) launch (args)Explanation / Answer
Given below is the modified code for the question. Request you to provide copyable code i.e text rather than image, so that it is easy to edit the existing code ,rather than type it out again.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class U4D1_HandleEvents extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Button btn = new Button();
btn.setText("Say 'Hello World'");
//using annonymous inner class
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.