java - EventHandler in JavaFX a class or interface -
java - EventHandler<T extends Event> in JavaFX a class or interface -
i have picked basic illustration of printing "hello world" on screen when mouse clicked code goes this.
package sample; 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; /** * * @author gauravp */ public class sample extends application { /** * @param args command line arguments */ button btn = new button("ok"); //label l = new label("done"); public static void main(string[] args) { launch(args); } @override public void start(stage primarystage) { primarystage.settitle("first stage"); //created anonymous inner class eventhandler<actionevent> btn.setonaction(new eventhandler<actionevent>() { @override public void handle(actionevent event) { system.out.print("hello world !!"); } }); stackpane root = new stackpane(); root.getchildren().add(btn); primarystage.setscene(new scene(root, 300, 250)); primarystage.show(); } }
in documentation mentioned eventhandler interface , how come interface instantiated...
"new eventhandler<actionevent>()"
in lot of confusion....please reply if have idea.. here link eventhandler interface : http://docs.oracle.com/javafx/2.0/api/javafx/event/eventhandler.html
the syntax
new eventhandler<actionevent>() { @override // <- notice annotation, overrides interface. public void handle(actionevent event) { system.out.print("hello world !!"); } }
creates "anonymous inner class" implements eventhandler, , defines handle method. if inspect classes generated when compile project, find class file named sample$1 (or similar) class generated code.
you can read on inner (anonymous) classes here: http://docs.oracle.com/javase/tutorial/java/javaoo/innerclasses.html
to reply question: eventhandler interface, , code doesn't create instance of it, instance of newly declared anonymous class.
java javafx
Comments
Post a Comment