0

I have a Course class with its constructor:

public class Course extends AbstractSubmission {


    private String course_content;

    public Course(String course_name, String username, String course_content) {

        this.set_course_name(course_name);
        this.set_username(username);
        this.set_ip_address("null");
        this.set_timestamp(new Date());
        this.set_course_content(course_content);

    }

    public Course(Course course) {

        this.set_course_id(course.get_course_id());
        this.set_course_name(course.get_course_name());
        this.set_username(course.get_username());
        this.set_ip_address(course.get_ip_address());
        this.set_timestamp(course.get_timestamp());
        this.set_course_content(course.get_course_content());

    }

I want to basically use the course class to initialize a javaFX tableview without changing the original constructor. I want to have a constructor with the table columns as arguments. I searched on wrapper methods/classes but I'm a little lost on how to do it. Please help :)

public class AdminAllCoursesController implements Initializable {
    @FXML private TableView<Course> coursesTable = new TableView<>();
    @FXML private TableColumn<Course, Integer> id;
    @FXML private TableColumn<Course, String> courseName;
    @FXML private TableColumn<Course, String> courseIP;
    @FXML private TableColumn<Course, Date> courseTimeStamp;
    @FXML private TableColumn<Course, String> addedBy;
    @FXML private TableColumn<Course, String> courseContent;
    @Override
    public void initialize(URL location, ResourceBundle resources) {

        //Using Simple Property to store and list course data
        id.setCellValueFactory(new PropertyValueFactory<Course, Integer>("id"));
        courseName.setCellValueFactory(new PropertyValueFactory<Course, String>("courseName"));
        courseContent.setCellValueFactory(new PropertyValueFactory<Course, String>("courseContent"));
        addedBy.setCellValueFactory(new PropertyValueFactory<Course, String>("addedBy"));
        courseIP.setCellValueFactory(new PropertyValueFactory<Course, String>("courseIP"));
        courseTimeStamp.setCellValueFactory(new PropertyValueFactory<Course, Date>("courseTimeStamp"));
        coursesTable.setItems(coursesList);

    }
}
Dro
  • 1
  • 1
  • 2
    What does the constructor have to do with this? It does not matter how the objects you add to a `TableView` are created. Since you're using `PropertyValueFactory` the only thing that matters is whether the appropriate getter methods exist or not. And in this case it seems like they do not exists in your class. Also `PropertyValueFactory` expects you to adhere to the java naming conventions for methods/getters which you do not: A `PropertyValueFactory` with `"courseName"` as parameter looks for a `getCourseName()` method to retrieve the value. `get_course_name()` won't do the trick. – fabian Apr 28 '19 at 00:28
  • Because I was hoping to keep the model classes, which Course is one of them, separate from the controller classes. I didn't want to set up the getters in the Course class if possible. – Dro Apr 28 '19 at 00:46
  • Passing the strings corresponding to the getters should work `new PropertyValueFactory("_course_name")`, if not, you could still implement your own factories: `cd -> new SimpleStringProperty(cd.getValue().get_course_name())`... – fabian Apr 28 '19 at 09:15
  • Possible duplicate of [Javafx tableview not showing data in all columns](https://stackoverflow.com/questions/18971109/javafx-tableview-not-showing-data-in-all-columns) – kleopatra Apr 28 '19 at 14:18
  • please learn java naming conventions and stick to them - usually, I prepend a "unrelated" to the advice, but this time seems to be (probably, will not try ;) at the heart of the problem. – kleopatra Apr 28 '19 at 14:20

0 Answers0