I am trying to read from a CSV file in one class, which loops through the contents of the CSV file which takes the form of Student Number in column 1 and Question 1 to Question xAmount in the columns and creates an object from the rows in the CSV file. In another class, I am creating a table and trying to dynamically populate the table using the contents of the CSV file.
So far I have set it up so that the columns dynamically populate based on the headers in the CSV file, however whenever it comes to the rows it throws an error: "WARNING: Can not retrieve property 'student number' in PropertyValueFactory: javafx.scene.control.cell.PropertyValueFactory@76afc271 with provided class type: class java.lang.String" and does this for each column in the CSV file. Is there anyway I can dynamically populate the rows?
Here is the code for the StudentCsv Class:
public class StudentCsv {
private int[] marks;
private String studentNumber;
public StudentCsv(String studentNumber, int[] marks) {
this.studentNumber=studentNumber;
this.marks = marks;
}
}
And here is the code for the tableView class:
public class tableViewTest implements Initializable {
@FXML
private TableView tableView;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
ArrayList<StudentCsv> testStudents = new ArrayList<>();
StudentCsv studentCsv1 = new StudentCsv("40236387", new int[]{2, 3, 4, 5, 6});
StudentCsv studentCsv2 = new StudentCsv("40236388", new int[]{7, 8, 9, 10, 11});
StudentCsv studentCsv3 = new StudentCsv("40236388", new int[]{12, 13, 14, 15, 16});
testStudents.add(studentCsv1);
testStudents.add(studentCsv2);
testStudents.add(studentCsv3);
String[] columnNames = {"Student number", "q1", "q2", "q3", "q4", "q5"};
ObservableList<StudentCsv> studentData = FXCollections.observableArrayList();
for (int i = 0; i < testStudents.size(); i++) {
studentData.add(testStudents.get(i));
}
tableView.setItems(studentData);
for (int i = 0; i < columnNames.length; i++) {
TableColumn<StudentCsv, String> column = new TableColumn<>(columnNames[i]);
column.setCellValueFactory(param ->
new ReadOnlyObjectWrapper<>(param.getValue()).asString());
column.setPrefWidth(75);
tableView.getColumns().add(column);
}
}
}
public class Main extends Application {
@Override
public void start(Stage stage) throws IOException {
try{
Parent root = FXMLLoader.load(getClass().getResource("tableViewTest.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}catch (Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
launch();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.testfx.tableViewTest">
<children>
<TableView fx:id="tableView" layoutX="43.0" layoutY="76.0" prefHeight="286.0" prefWidth="505.0" />
</children>
</AnchorPane>