I have picked a basic example of printing "Hello World" on screen when the mouse is clicked The code goes like 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 the 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 it is mentioned that EventHandler is an interface , but how come the interface be instantiated...
"new EventHandler<ActionEvent>()"
In a lot of confusion....please reply if you have any idea.. Here is the link for the EventHandler interface : http://docs.oracle.com/javafx/2.0/api/javafx/event/EventHandler.html