Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Custom Event, EventType and EventHandler

934053Apr 30 2012 — edited May 1 2012
Hi,

I am currently trying to write a custom event, event type and event handler. I have based my code on the ActionEvent.

If I run the code below and press the button the output is

ActionEvent 1
ActionEvent 2

rather than

ActionEvent 1
ActionEvent 2
MyEvent 1
MyEvent 2

Do I need to register my Event and EventType somehow for this to work?
package customeventfx;

import javafx.application.Application;
import javafx.event.*;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class CustomEventFX extends Application {

    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Custom JavaFX Event");
        Button btn = new Button();
        btn.setId("Fire Button");
        btn.setText("Fire MyEvent'");
        
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                
                if (event.getEventType().equals(ActionEvent.ACTION)) {
                    System.out.println("ActionEvent 1");
                }
            }
        });    
        btn.addEventHandler(ActionEvent.ACTION, new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                   
                if (event.getEventType().equals(ActionEvent.ACTION)) {
                    System.out.println("ActionEvent 2");
                }
            }     
        }); 
     
        // Custom Code
        btn.addEventHandler(MyEvent.BUTTON_PRESSED, new EventHandler<MyEvent>() {           

            @Override
            public void handle(MyEvent event) {
                   
                if (event.getEventType().equals(MyEvent.BUTTON_PRESSED)) {
                    System.out.println("MyEvent 1");
                }
            }     
        });  
        btn.addEventHandler(MyEvent.BUTTON_PRESSED, new MyEventHandler());
        
        StackPane root = new StackPane();
        root.getChildren().add(btn);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }
}

// The Handler
class MyEventHandler implements EventHandler<MyEvent> {
      
    @Override
    public void handle(MyEvent event) {
        
        if (event.getEventType().equals(MyEvent.BUTTON_PRESSED)) {
            System.out.println("MyEvent 2");
        }
    }
}

// The Event
class MyEvent extends Event {
    
    public static final EventType<MyEvent> BUTTON_PRESSED = new EventType(ANY, "BUTTON_PRESSED");
    
    public MyEvent() {
        this(BUTTON_PRESSED);
    }
    
    public MyEvent(EventType<? extends Event> arg0) {
        super(arg0);
    }
    public MyEvent(Object arg0, EventTarget arg1, EventType<? extends Event> arg2) {
        super(arg0, arg1, arg2);
    }  
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 29 2012
Added on Apr 30 2012
3 comments
6,591 views