Creating a EventHandler class for buttons with different actions

Posted on

Problem

I have the following code at the moment:

public void initialiseButtons() {
    this.saveNewButton.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent actionEvent) {
            savePatientData(actionEvent);
        }
    });
    this.clearFieldsButton.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent actionEvent) {
            TextFieldHelpers.clearInputFields(dataInputFields);

        }
    });
    this.cancelNewPatientButton.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent actionEvent) {
            clearWorkspace();
        }
    });
    this.saveChangesButton.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent actionEvent) {
            savePatientData(actionEvent);
        }
    });
    this.deletePatientButton.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent actionEvent) {
            deleteExistingPatient();
        }
    });
    this.deselectPatientButton.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent actionEvent) {
            patientListView.getSelectionModel().clearSelection();
        }
    });
    addNewButton.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            setupViewForEditingOrAddingPatientAction(1);
        }
    });
}

As you can see I have repeated the creation of a EventHandler numerous amount of times. Can someone guide me on how I can minimise this or is this the only way?

Solution

Assuming that you have Java 8 available:

this.saveNewButton.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent actionEvent) {
        savePatientData(actionEvent);
    }
});

Can become, using a lambda:

this.saveNewButton.setOnAction(actionEvent -> savePatientData(actionEvent));

Or, for that particular event handler:

this.saveNewButton.setOnAction(this::savePatientData);

Example of another EventHandler

this.deletePatientButton.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent actionEvent) {
        deleteExistingPatient();
    }
});

Can become:

this.deletePatientButton.setOnAction(event -> deleteExistingPatient());

If you do not have Java 8 available:

Sorry, you’re pretty much screwed. 🙁

Essentially, there is not that much code duplication as you might think, as each EventHandler is different.

The only way possible to solve it in this case would be to have all the buttons use the same EventHandler and perform a whole lot of checks such as:

if (event.getSource() == this.deletePatientButton) {
    ...
}

Side note:

If you are using JavaFX 8, you have the possibility to define an onAction in your FXML for your buttons, this will be associated with a method of that name in your controller.

See also http://code.makery.ch/blog/javafx-8-event-handling-examples/

Leave a Reply

Your email address will not be published. Required fields are marked *