如何将具有可变大小的嵌套对象数组设置为 javafx TableView

我有一个 ClassTestEvaluation 类型的对象。它拥有一个 Task 类型的 ArrayList。这个数组可以有任意数量的任务并且是不可预测的。我想设置一个表格视图,它将在列中显示给定的任务,以便用户可以输入分数。


我的问题是我不知道如何将一个可变长度的数组设置为另一个对象的一部分作为我的表格视图的源。我无法使用简单的成员 task1、task2 等构建像 ClassTestEvaluationItem 这样的包装器,因为任务的数量不固定。因此,我的问题是我必须做什么才能正确设置任务列的 propertyValueFactories?


我遵循了很多教程,比如这里的教程:https ://docs.oracle.com/javafx/2/ui_controls/table-view.htm 。一般来说,跟随并让他们工作是没有问题的。但它们只处理简单(平面)数据。


TableView<ClassTestEvaluation> table = new TableView<>();

TableColumn colFirstName = new TableColumn("Vorname");

colFirstName.setCellValueFactory(new PropertyValueFactory<ClassTestEvaluation, String>("firstName");

TableColumn colLastName = new TableColumn("Nachname");

colLastName.setCellValueFactory(new PropertyValueFactory<ClassTestEvaluation, String>("lastName");


table.getColumns().addAll(colFirstName, colLastName);


ArrayList<Task> tasks = classTestEvaluation.getTasks();

for(Task t : tasks) {

    TableColumn colTask = new TableColumn(t.getTitle());

    table.getColumns().add(colTask);

}

如您所见,为 colFirstName 和 colLastName 设置 PropertyValueFactory 并不难。但是对于长度可变并存储在本身是 ClassTestEvaluation 对象的一部分的数组中的任务,我该怎么做呢?


根据 classtest 的任务数量,我希望我的表格视图看起来像这样:


名字 | 姓氏 | 任务 1 | 任务 2 | ...


我希望有人可以在这里帮助我,因为我不明白。非常感谢!


湖上湖
浏览 93回答 1
1回答

慕妹3242003

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

相关分类

Java