I am new to Java and OOP and got stuck in adding image to tableview column. Code seems to work, I can see the name of the student correct but images are not shown in the column. I am getting this error and could not understand how to make it work:
javafx.scene.control.cell.PropertyValueFactory getCellDataReflectively
WARNING: Can not retrieve property 'picture' in PropertyValueFactory: javafx.scene.control.cell.PropertyValueFactory@5b0da50f with provided class type: class model.StudentModel
java.lang.IllegalStateException: Cannot read from unreadable property picture
StudentModel:
package model;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.image.ImageView;
import java.util.ArrayList;
import java.util.List;
public class StudentModel {
private ImageView picture;
private String name;
private SubjectModel major;
private SubjectModel minor;
private String accountPassword;
public String getAccountPassword()
{
return accountPassword;
}
public List<LectureModel> lectureModelList = new ArrayList<>();
public StudentModel(String name, SubjectModel major, SubjectModel minor, ImageView picture, String accountPassword)
{
this.name = name;
this.major = major;
this.minor = minor;
this.picture = picture;
this.accountPassword = accountPassword;
}
public String getName()
{
return name;
}
public ObservableList<LectureModel> myObservableLectures(){
ObservableList<LectureModel> observableList = FXCollections.observableArrayList(lectureModelList);
return observableList;
}
public ImageView getPhoto(){
return picture;
}
public void setPhoto(ImageView photo)
{
this.picture = photo;
}
}
And Participants Scene which I have the tableview:
public class ParticipantsScene extends Scene {
private final StudentController studentController;
private final ClientApplication clientApplication;
private final TableView<StudentModel> allParticipantsTable;
private final ObservableList<StudentModel> enrolledStudents;
private LectureModel lecture;
public ParticipantsScene(StudentController studentController, ClientApplication application, LectureModel lecture) {
super(new VBox(), 800 ,500);
this.clientApplication = application;
this.studentController = studentController;
this.lecture = lecture;
enrolledStudents=lecture.observeAllParticipants();
TableColumn<StudentModel, String > nameCol = new TableColumn<>("Name");
nameCol.setMinWidth(200);
nameCol.setCellValueFactory(new PropertyValueFactory<>("name"));
TableColumn<StudentModel, ImageView> picCol = new TableColumn<>("Images");
picCol.setPrefWidth(200);
picCol.setCellValueFactory(new PropertyValueFactory<>("picture"));
allParticipantsTable = new TableView<>();
allParticipantsTable.getColumns().addAll(nameCol,picCol);
allParticipantsTable.setItems(enrolledStudents);
VBox vBox = new VBox(10, allParticipantsTable, createButtonBox());
vBox.setAlignment(Pos.CENTER);
setRoot(vBox);
}
private HBox createButtonBox() {
var backButton = new Button("Back");
backButton.setOnAction(event -> clientApplication.showAllLecturesScene());
var buttonBox = new HBox(10, backButton);
buttonBox.setAlignment(Pos.CENTER);
return buttonBox;
}
}
Also adding Lectures model in case it may helpful:
public class LectureModel {
private String lectureName;
private String lectureHall;
private String subjectName;
private SubjectModel subject;
private TimeSlot timeSlot;
//private Button actionButton1;
//private Button actionButton2;
private List<StudentModel> enrolledStudents = new ArrayList<>();
private String name;
public LectureModel(String lectureName, String lectureHall, SubjectModel subject, TimeSlot timeSlot){
this.lectureName = lectureName;
this.lectureHall = lectureHall;
this.subject = subject;
this.timeSlot = timeSlot;
this.subjectName = this.subject.getSubjectName();
}
public String getLectureName()
{
return lectureName;
}
public String getLectureHall()
{
return lectureHall;
}
public SubjectModel getSubject()
{
return subject;
}
public String getSubjectName()
{
return subjectName;
}
public List<StudentModel> getEnrolledStudents()
{
return enrolledStudents;
}
public ObservableList<StudentModel> observeAllParticipants() {
ObservableList<StudentModel> observableList = FXCollections.observableArrayList(getEnrolledStudents());
return observableList;
}
public TimeSlot getTimeSlot() {
return timeSlot;
}
public void addStudent(StudentModel studentModel){ enrolledStudents.add(studentModel);}
public void removeStudent(StudentModel studentModel)
{
enrolledStudents.remove(studentModel);
};
Appreciate any kind of helps, Thanks!