EventHandler<T extends Event> in JavaFX a class or interface.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gaurav Pruthi
    New Member
    • Dec 2011
    • 7

    #1

    EventHandler<T extends Event> in JavaFX a class or interface.

    Hi,

    I have picked a basic example of printing "Hello World" on screen when the mouse is clicked The code goes like this.

    Code:
    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<Ac tionEvent>()"

    In a lot of confusion....pl ease reply if you have any idea.. Here is the link

    http://docs.oracle.com/javafx/2.0/api/javafx/event/EventHandler.ht ml
Working...