1 回答
TA贡献1824条经验 获得超6个赞
好吧,我实际上找到了自己问题的解决方案,并认为这可能会引起其他人的兴趣。回想一下:我的问题是我不知道如何从可变长度的数组中构建列,该数组是另一个项目(组合)的一部分。
我需要了解的是 cellValueFactory、cellFactory 和 onEditCommit 如何分别与回调或 lambda 一起使用以获得所需的结果。这花了我相当长的时间。
这是我为得到它所做的:
private void createTableColumnsForTasks(ClassTestEvaluation cte) {
int resultCounter = 1;
// For each given task in ClassTestEvaluation...
for (Task t : cte.getTasks()) {
// ...create a column for the corresponding points reached (0 in the beginning).
TableColumn<ClassTestEvaluationRow, Double> colResult = new TableColumn<>("A" + resultCounter + " (" + t.getPoints() + ")");
resultCounter++;
// Make the column editable.
colResult.setEditable(true);
// Define how the values in the task columns will be set.
colResult.setCellValueFactory((TableColumn.CellDataFeatures<ClassTestEvaluationRow, Double> row) -> {
ClassTestEvaluationRow cter = row.getValue();
for(Result r : cter.getResults()) {
if(r.getIdTask() == t.getId()) {
ObservableValue<Double> obsResult = new SimpleObjectProperty<Double>(r.getPoints());
return obsResult;
}
}
return null;
});
// Create combo box to choose points from for each given task and result.
createTableColumnComboBoxForResultEditing(colResult, t);
// Create method to store edited data in selected ClassTestEvaluationRow.
createOnEditCommitMethods(colResult, t);
((EvaluatorUI) this.getView()).getTableView().getColumns().add(colResult);
}
}
private void createTableColumnComboBoxForResultEditing(TableColumn<ClassTestEvaluationRow, Double> colResult, Task t) {
ArrayList<Double> cbPoints = new ArrayList<>();
for(double i = 0.0; i <= t.getPoints(); i = i+0.5) {
cbPoints.add(i);
}
ObservableList<Double> obsPoints = FXCollections.observableArrayList(cbPoints);
colResult.setCellFactory(ComboBoxTableCell.<ClassTestEvaluationRow, Double>forTableColumn(obsPoints));
}
private void createOnEditCommitMethods(TableColumn<ClassTestEvaluationRow, Double> colResult, Task t) {
colResult.setOnEditCommit((CellEditEvent<ClassTestEvaluationRow, Double> event) -> {
TablePosition<ClassTestEvaluationRow, Double> pos = event.getTablePosition();
Double newValue = event.getNewValue();
int row = pos.getRow();
ClassTestEvaluationRow cter = (ClassTestEvaluationRow) event.getTableView().getItems().get(row);
for(Result result : cter.getResults()) {
if(result.getIdTask() == t.getId()){
result.setPoints(newValue);
classTestEvaluationsService.updateResult(cter.getClassTestEvaluation(), result);
}
}
});
}
我希望这对你们中的一些人有所帮助。问候。
添加回答
举报