0

I have looked at other similar problems on this website, however none of the solutions seem to apply to me. I have already checked the flagged answer and updated the comments, however the program is still not working.

I am trying to make a Scheduler Application. I am running into a problem with adding data into my TableView in JavaFX. This is the main program below:

public class SchedulerController implements Initializable {

/**
 * Initializes the controller class.
 */
//configures the table
@FXML
private TableView<ScheduleTask> tableView;
@FXML
private TableColumn<ScheduleTask, String> timeColumn;
@FXML
private TableColumn<ScheduleTask, String> descriptionColumn;

@Override
public void initialize(URL url, ResourceBundle rb) {
    // this sets up the columns in the tableview

    timeColumn.setCellValueFactory(new PropertyValueFactory<ScheduleTask, String>("scheduleTime"));
    descriptionColumn.setCellValueFactory(new PropertyValueFactory<ScheduleTask, String>("scheduleName"));

}

public void initData(Object[] tasks) {

    TodoTask t;

    //iterate around array of tasks adding to the scheduler
    for (int i = 0; i < tasks.length; i++) {

        t = (TodoTask) tasks[i];
        t.getDueDate();
        t.getNumHours();

        int hoursForTask = Integer.parseInt(t.getNumHours());
        String nameofScheduleTask = (t.getTodoName());

        LocalDate today = LocalDate.now();
        LocalDate futureDue = t.getDueDate();

        double daystoDue = ChronoUnit.DAYS.between(today, futureDue);
        double timeForTask = hoursForTask / daystoDue;
        String timeForTaskString = Double.toString(timeForTask);

//**This is the main part where I input the data into the Scheduler, and 
//nothing comes up**
        ScheduleTask newScheduleTask = new ScheduleTask(t.getTodoName(),t.getNumHours());
        tableView.getItems().add(newScheduleTask);
}

}

I have commented on the part where I think there is a problem. The above program is the Controller for the Scheduler Screen, and the below code links the Scheduler screen to the other screen where the data is inputted, when the user presses 'Change Screen' on my program:

public void changeScreen(ActionEvent event) throws IOException {
    FXMLLoader loader = new FXMLLoader(getClass().getResource(
  "Scheduler.fxml"
    ));

     Parent root = (Parent) loader.load();

    Parent schedulerParent = FXMLLoader.load(getClass().getResource("Scheduler.fxml"));
    Scene schedulerScene = new Scene(schedulerParent);

    Stage window = (Stage) ((Node) event.getSource()).getScene().getWindow();
    window.setScene(schedulerScene);

    SchedulerController newcontroller = 
    loader.<SchedulerController>getController();

    ObservableList ol=todoView.getItems();
    Object[] ob=ol.toArray();

    newcontroller.initData(todoView.getItems().toArray());

    window.show();

}

I have tried to pass the objects through an array to the program above, however whenever I add a new Task and change screen to the Scheduler, there is no task added. I also already have added getters and setters in other classes, and there seem to be no problems with that. I have attached it below:

public class ScheduleTask {
private SimpleStringProperty scheduleName,scheduleTime;

public ScheduleTask(String scheduleName, String scheduleTime) {
    this.scheduleName = new SimpleStringProperty (scheduleName); 
    this.scheduleTime = new SimpleStringProperty(scheduleTime);
}

public String getScheduleName() {
    return scheduleName.get();
}

public void setScheduleName(SimpleStringProperty scheduleName) {
    this.scheduleName = scheduleName;
}

public String getScheduleTime() {
    return scheduleTime.get();
}

public void setScheduleTime(SimpleStringProperty scheduleTime) {
    this.scheduleTime = scheduleTime;
}

}

I am not too experienced with the website, so please let me know if you need further clarification or if anything is unclear. Thank you.

dp1895
  • 23
  • 4
  • Possible duplicate of [Javafx PropertyValueFactory not populating Tableview](https://stackoverflow.com/questions/17035478/javafx-propertyvaluefactory-not-populating-tableview) – Itai Sep 23 '17 at 08:14
  • See flagged answer. If the getter is `getScheduleTime` then the `PropertyValueFactory` should be `"scheduleTime"` not just `"time"`. – Itai Sep 23 '17 at 08:15
  • @sillyfly I have changed this, there doesn't seem to be any difference. Do I need to do anything else? – dp1895 Sep 23 '17 at 08:22
  • It seems you also load your FXML twice - you add to the scene the parent from one, yet access the controller of the other. Try removing the line `Parent schedulerParent = FXMLLoader.load(getClass().getResource("Scheduler.fxml"));`, and change the next one so the scene contains the previously-loaded `root` as the root node. – Itai Sep 23 '17 at 08:43
  • @sillyfly that fixed it! Thank you so much – dp1895 Sep 23 '17 at 09:06

0 Answers0