如何使用 JavaFX 中的按钮将数据从组合框添加到 Table 列?

How to add data from Combo Box to Table Column using a Button in JavaFX?

所以我有以下代码:

public class ToDoListController implements Initializable {

    String text = "";
    LocalDate isoDate;

    @FXML
    private Button bttnAddEvent;

    @FXML
    private DatePicker pickerDate;

    @FXML
    private ComboBox<String> eventsSelector;

    @FXML
    private Button bttnDone;

    @FXML
    private Button bttnRemove;

    @FXML
    private TableView<EventsBean> eventsTable;

    @FXML
    private TableColumn<EventsBean, String> eventCol;

    @FXML
    private TableColumn<EventsBean, LocalDate> dateCol;

    @FXML
    private TableColumn<?, ?> doneCol;

    @FXML
    private TableColumn<?, ?> observationCol;

    @FXML
    private TableColumn<?, ?> removeCol;

    @FXML
    void bttnDoneAction(ActionEvent event) {

    }

    @FXML
    void bttnRemoveAction(ActionEvent event) {

    }

    @FXML
    void pickerDateAction(ActionEvent event) {
         isoDate = pickerDate.getValue();
    }

            ObservableList<EventsBean> dataList = 
    FXCollections.observableArrayList();


    @Override
    public void initialize(URL location, ResourceBundle resources) {
    System.out.println("The pane loaded");

    List<String> myList;
    try {
        myList = Files.lines(Paths.get("src/com/todolist/EventsList.txt")).collect(Collectors.toList());
        eventsSelector.setItems(FXCollections.observableArrayList(myList));
    } catch (IOException e) {
        System.out.println("Don t find file");
    }

    eventCol.setCellValueFactory(new PropertyValueFactory<EventsBean, String>("event"));
    dateCol.setCellValueFactory(new PropertyValueFactory<EventsBean, LocalDate>("date"));

    observationCol.setCellValueFactory(new PropertyValueFactory<EventsBean, String>("observation"));
    observationCol.setCellFactory(TextFieldTableCell.<EventsBean>forTableColumn());
    observationCol.setOnEditCommit(
            (CellEditEvent<EventsBean, String> t) -> {
                ((EventsBean) t.getTableView().getItems().get(
                    t.getTablePosition().getRow())
                    ).setObservation(t.getNewValue());
        });
    observationCol.setSortable(false);

    eventsTable.setItems(dataList);
    eventsTable.setEditable(true);

    bttnAddEvent.setOnAction((ActionEvent e) -> {
        text = eventsSelector.getValue().toString();
        dataList.add(new EventsBean(text, isoDate, ""));
    });

    }
    }

和我的 EventsBean class:

public class EventsBean {

private SimpleStringProperty observation;
private SimpleStringProperty event;
private SimpleObjectProperty<LocalDate> date;

public EventsBean(String event, LocalDate date, String observation) {
    this.event = new SimpleStringProperty(event);
    this.date = new SimpleObjectProperty<LocalDate>(date);      
    this.observation = new SimpleStringProperty(observation);
}
public String getEvent() {
    return event.get();
}

public LocalDate getDate() {
    return date.get();
}
public String getObservation() {
    return observation.get();
}

public void setObservation(String observation) {
    this.observation.set(observation);
}
}

如您所见,我用文本文件中的数据填充了 ComboBox。 所以我想使用 Button bttnAddEventComboBox 添加选定的数据到 TableColumn <?,?> eventCol。除此之外,另一个挑战是在同一时间使用相同的 Button bttnAddEvent 和来自 ComboBox 的数据,以在 TableColumn <?,?> dateCol.[=21= 中添加来自 DatePicker pickerDate 的选定日期]

更新:到现在为止我把ComboBox的值放到了一个名为text的字符串中,但是如何把这个值从text传到TableColumn eventCol中呢?

非常感谢!

我找到了解决方案并为满足此需求的人更新了代码。