使用Scenebuilder在JavaFX中使用CellValueFactory将数据输入到TableView中

时间:2017-09-23 08:08:14

标签: java tableview scenebuilder

我在这个网站上看过其他类似的问题,但是这些解决方案似乎都不适用于我。我已经检查了标记的答案并更新了评论,但该程序仍然无效。

我正在尝试制作一个Scheduler应用程序。我遇到了在JavaFX中将数据添加到我的TableView中的问题。这是下面的主要计划:

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);
}

}

我评论了我认为存在问题的部分。以上程序是调度程序屏幕的控制器,下面的代码将调度程序屏幕链接到数据的另一个屏幕当用户在我的程序上按下“更改屏幕”时输入:

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();

}

我试图通过数组将对象传递给上面的程序,但每当我添加一个新的Task并将屏幕更改为Scheduler时,都没有添加任务。我也已经在其他类中添加了getter和setter,并且似乎没有问题。我在下面附上了它:

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;
}

}

我对网站没有太多经验,所以如果您需要进一步澄清或有任何不清楚的地方,请告诉我。谢谢。

0 个答案:

没有答案