I have two methods in my JavaFX-code for creating TextFields with Bindings. I have to bind ReadOnlyProperty or StringBinding. So I created two methods with different method signatures but with the same code in the block.
How can I simplify my code (with generics?) to handle all the different Properties (String, Long, Object etc.)?
createTextField(knkFile.idProperty().asString(), 1, 0); // ReadOnlyIntegerProperty
createTextField(knkFile.dateProperty().asString(), 1, 1); // ReadOnlyObjectProperty
createTextField(knkFile.fileNameProperty(), 1, 2); // ReadOnlyStringProperty
createTextField(knkFile.lastModifiedProperty().asString(), 1, 5); // ReadOnlyLongProperty
private void createTextField(ReadOnlyProperty property, int column, int row) {
TextField textField = new TextField();
textField.textProperty().bind(property);
this.add(textField, column, row); // add to GridPane
}
private void createTextField(StringBinding binding, int column, int row) {
TextField textField = new TextField();
textField.textProperty().bind(binding);
this.add(textField, column, row);
}
Edit: own answer (deprecated - see accepted answer)
I just saw that StringBinding and ReadOnlyProperty implement ObservableValue. So I only need this method now:
private void createTextField(ObservableValue property, int column, int row) {
TextField textField = new TextField();
textField.textProperty().bind(property);
this.add(textField, column, row);
}
