Reputation: 2294
I have a dozen of parameters i want to display throught a javafx application. I just pick TableView to render these parameters.
Here is how theses parameters looks like :
public abstract class AbstractParameter<T> {
private String myKey;
private ObjectProperty<T> myValue = new SimpleObjectProperty<T>()
private boolean constantValue;
public AbstractParameter<T>(){
//...
}
ObjectProperty<T> property(){
return property;
}
}
Note here that a have multiple subclasses such as StringParameter, IntegerParameter, LongParameter, BooleanParameter, ect...
I'm using that approach because some of the params are defined/updated during runtime
Now here is my ParametersUIController
public Class ParametersUiController {
@FXML
private TableView<AbstractParameter<?>> paramsTable;
@FXML
private TableColumn<AbstractParameter<?>, String> paramKeyCol;
@FXML
private TableColumn<AbstractParameter<?>, String> paramValueCol;
@FXML
private TableColumn<AbstractParameter<?>, Boolean> paramConstantValueCol;
private ObservableList<AbstractParameter<?>> paramsData = FXCollections.observableArrayList();
/**
* Just add some sample data in the constructor.
*/
public PersonTableController() {
/* getting a list of params for a service
the params are defined in list
*/
for(AbstractParameter<?> param : rawData)
paramsData.add(param);
}
/* I cast the value type to string always */
pValueCol.setCellValueFactory(new Callback<CellDataFeatures<AbstractParameter<?>, String>, ObservableValue<String>>() {
public ObservableValue<String> call(CellDataFeatures<AbstractParameter<?>, String> param) {
return new SimpleStringProperty(param.getValue().property().get().toString());
}
});
}
I'm wordering is there any other proper way to display generic types T without casting a defined paramter at runtime with String every time ?
Upvotes: 1
Views: 186
Reputation: 1668
You can use this:
pValueCol.setCellValueFactory(new PropertyValueFactory("generic"));
and create a method like this in your model:
ObjectProperty<T> genericProperty(){
return property;
}
The statement new PropertyValueFactory("generic")
look for a method in your model named genericProperty
. It can return the generic value wrapped in an ObjectProperty<T>
.
Upvotes: 1