I have a TableView
control which consists of several columns containg different types including String
s and Number
s. I have been trying to write a suitable callback function for an editable Number cell, but I can't get anywhere with it as I get a variety of issues ranging from empty cells to exceptions.
I have read through http://docs.oracle.com/javafx/2/ui_controls/table-view.htm but this only covers String values in cells. The sticking point seems to be lastNameCol.setCellFactory(TextFieldTableCell.forTableColumn());
. This line seems to be tailored for Text fields and not for Number fields.
Also, I wish to perform validation on the numbers entered. Does this require a custom callback for the CellFactory
in order to do this? If so, how can I develop a callback that accepts Number types and validates them?
Here's a code snippet of what I currently have in my project:
@FXML private TableView<BMIRecord> fxBMITable;
@FXML private TableColumn<BMIRecord, String> fxBMITableDate;
@FXML private TableColumn<BMIRecord, String> fxBMITableTime;
@FXML private TableColumn<BMIRecord, Number> fxBMITableHeight;
@FXML private TableColumn<BMIRecord, Number> fxBMITableWeight;
@FXML private TableColumn<BMIRecord, Number> fxBMITableBMI;
// ...
private void someFunc() {
fxBMITable.setEditable(true);
/* BMI table callback configuration */
fxBMITableHeight.setCellValueFactory(new Callback<CellDataFeatures<BMIRecord, String>, ObservableValue<String>>() {
public ObservableValue<String> call(CellDataFeatures<BMIRecord, String> p) {
return new SimpleStringProperty(p.getValue().getDateString());
}
});
/*
* ERROR:
* The method setCellFactory(Callback<TableColumn<BMIRecord,Number>,TableCell<BMIRecord,Number>>)
* in the type TableColumn<BMIRecord,Number> is not applicable for the arguments
* (Callback<TableColumn<Object,String>,TableCell<Object,String>>)
*/
fxBMITableHeight.setCellFactory(TextFieldTableCell.forTableColumn());
fxBMITableHeight.setOnEditCommit(new EventHandler<CellEditEvent<BMIRecord, Number>>() {
@Override
public void handle(CellEditEvent<BMIRecord, Number> t) {
((BMIRecord)t.getTableView().getItems().get(t.getTablePosition().getRow())).setHeight(t.getNewValue().doubleValue());
}
});
}
Thanks for any help in advance.
<BMIRecord, Number>
part gets rejected. Without that, I get an error similar to before: "The method setCellFactory(Callback<TableColumn<BMIRecord,Number>,TableCell<BMIRecord,Number>>) in the type TableColumn<BMIRecord,Number> is not applicable for the arguments (Callback<TableColumn<Object,Number>,TableCell<Object,Number>>)" – Paneling