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!

getPhotoandsetPhototogetPictureandsetPicture, respectively. Or usenew PropertyValueFactory<>("photo");. That should get your code working.PropertyValueFactory. And you might want to also check out the first half of this tutorial to understand how to use JavaFX properties. On a design level, your model class should not have anImageView. That's a view class and as such belongs in the view. You could have anImagein the model or, probably better, simply the URL/path to the image in the model.ParticipantsSceneis only configuring aScene, not adding behavior. That can be done in a dedicated "view class" or even just a method (e.g., in theApplication#start(Stage)method override for relatively simply applications).Imagein any of theImageViews in the code you've provided. This leads me to assume theImageViewis being displayed in the table cell (pretty sure the defaultcellFactorywill simply set the cell'sgraphicproperty if the item is aNode), but that you haven't given it anImageto render.