5
\$\begingroup\$

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?

\$\endgroup\$
5
\$\begingroup\$

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/

\$\endgroup\$
  • \$\begingroup\$ Ah this is perfect, I should have specified Java 8 :) \$\endgroup\$ – orange Feb 12 '15 at 16:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.