patternjavaMinor
Creating a EventHandler class for buttons with different actions
Viewed 0 times
eventhandleractionscreatingwithbuttonsdifferentforclass
Problem
I have the following code at the moment:
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?
public void initialiseButtons() {
this.saveNewButton.setOnAction(new EventHandler() {
@Override
public void handle(ActionEvent actionEvent) {
savePatientData(actionEvent);
}
});
this.clearFieldsButton.setOnAction(new EventHandler() {
@Override
public void handle(ActionEvent actionEvent) {
TextFieldHelpers.clearInputFields(dataInputFields);
}
});
this.cancelNewPatientButton.setOnAction(new EventHandler() {
@Override
public void handle(ActionEvent actionEvent) {
clearWorkspace();
}
});
this.saveChangesButton.setOnAction(new EventHandler() {
@Override
public void handle(ActionEvent actionEvent) {
savePatientData(actionEvent);
}
});
this.deletePatientButton.setOnAction(new EventHandler() {
@Override
public void handle(ActionEvent actionEvent) {
deleteExistingPatient();
}
});
this.deselectPatientButton.setOnAction(new EventHandler() {
@Override
public void handle(ActionEvent actionEvent) {
patientListView.getSelectionModel().clearSelection();
}
});
addNewButton.setOnAction(new EventHandler() {
@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:
Can become, using a lambda:
Or, for that particular event handler:
Example of another EventHandler
Can become:
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
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:
Side note:
If you are using JavaFX 8, you have the possibility to define an
See also http://code.makery.ch/blog/javafx-8-event-handling-examples/
this.saveNewButton.setOnAction(new EventHandler() {
@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() {
@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/
Code Snippets
this.saveNewButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
savePatientData(actionEvent);
}
});this.saveNewButton.setOnAction(actionEvent -> savePatientData(actionEvent));this.saveNewButton.setOnAction(this::savePatientData);this.deletePatientButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
deleteExistingPatient();
}
});this.deletePatientButton.setOnAction(event -> deleteExistingPatient());Context
StackExchange Code Review Q#80368, answer score: 5
Revisions (0)
No revisions yet.