How to implement a really custom event handler that can handle any EventType in JavaFX?


Question

I am planning to create a single EventHandler Class that will handle all Types of events for all my controls in my JavaFX class.

For example, I added my custom event handler class to handle the Action Event the following way and it just works fine:

userNameText.addEventHandler(ActionEvent.ACTION, new DataChangeHandler());
cmbBox.addEventHandler(ActionEvent.ACTION, new DataChangeHandler());
btn.addEventHandler(ActionEvent.ACTION, new DataChangeHandler());

Here is my custome event handler class code:

public class DataChangeHandler implements EventHandler<ActionEvent> {    
    public void handle(ActionEvent event) {
            System.out.println("My Very Own Private Button Handler");
        }               
}

But when I try to change one of the addEventHandlers to MouseEvent and modify the main EventHandler class the following way, it shows an error "The Interface eventhandler cannot be implemented more than once with different arguements":

userNameText.addEventHandler(ActionEvent.ACTION, new DataChangeHandler());
cmbBox.addEventHandler(MouseEvent.CLICKED, new DataChangeHandler());
btn.addEventHandler(ActionEvent.ACTION, new DataChangeHandler());

public class DataChangeHandler implements EventHandler<ActionEvent>, EventHandler<MouseEvent> {    
    public void handle(ActionEvent event) {
            System.out.println("My Very Own Private Button Handler - ACTIONEVENT");
        }

        @Override
    public void handle(MouseEvent arg0) {
        System.out.println("My Very Own Private Button Handler - MOUSEEVENT");

    }
}

Is there any other way to achieve this? Please help. Thanks in advance.

1
2
10/10/2012 12:13:52 AM

Try this

public class DataChangeHandler implements EventHandler<Event>{
    @Override
    public void handle(Event event) {
        System.out.println("My Very Own Private Handler For All Kind Of Events");
    }
}
6
10/10/2012 7:36:11 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon