传递一个ObservableList javaFX

时间:2018-06-13 06:15:30

标签: javafx javafx-8

这是我的Controller类,这个类包含从ArrayList添加,编辑和删除任务的方法,以及ObservableList的getter。

    public class taskController {
       @FXML  private Button addButton;       
       @FXML  private DatePicker datePicker = new DatePicker();
       @FXML  private TextField descriptionTextField = new TextField();
              private List<task> taskList= new ArrayList<task>();

    public void initialize() {
         datePicker.setValue(LocalDate.now());

    public taskController() {
    }

    public void showAddTask() throws IOException {
        Stage stage = new Stage();
        AnchorPane taskLayout;
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(taskController.class.getResource("addTask.fxml"));
        taskLayout = loader.load();
        Scene scene = new Scene(taskLayout);
        stage.setScene(scene);
        stage.show();

    }

   @FXML private void addTask() throws IOException, InterruptedException {
    this.taskList.add(new task(descriptionTextField.getText(),datePicker.getValue().getYear(),datePicker.getValue().getMonthValue(),datePicker.getValue().getDayOfMonth()));
    }

public ObservableList<task> getTaskListView() {
    return FXCollections.observableArrayList(this.taskList);
}

当我从Main类中获取ObservableList时,我收到此错误。

`Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at view.taskController.getTaskListView(taskController.java:93)
at Main.showTask(Main.java:49)
at Main.updateButton(Main.java:61)`

这是我的主要课程:

    public class Main extends Application {
          private taskController taskController = new taskController();       
          private feedController feedController = new feedController();
          private Stage primaryStage;
    @FXML private StackPane taskStackPane = new StackPane();
    @FXML private StackPane newsStackPane = new StackPane();
    @FXML private ListView<task> taskListView;
    @FXML private ListView<Calls> callList;

    public void initialize() {
        this.primaryStage = new Stage();
        showNews();
    }

    public void start(Stage primaryStage) throws IOException {
        VBox mainLayout;
        this.primaryStage = primaryStage;
        this.primaryStage.setTitle("App POO");
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("assistant.fxml"));
        mainLayout = loader.load();
        Scene scene = new Scene(mainLayout);
        primaryStage.setScene(scene);
        primaryStage.show();    
    }

          private void showNews() {
              newsStackPane.getChildren().add(feedController.generateObservableNews());         
          }
     private void showTask() {
         taskListView = new ListView<>();
         taskListView.setItems(this.taskController.getTaskListView());   
         taskStackPane.getChildren().add(taskListView);

     }

     public void newTaskButton() throws IOException {
            this.taskController.showAddTask();

        }

我不明白问题出在哪里,对象很好地实例化(这样认为),但是taskList正在丢失。

0 个答案:

没有答案