如何使用JavaFX中的Button将数据从组合框添加到表列?

时间:2017-06-09 09:09:09

标签: button javafx combobox tableview tablecolumn

所以我有以下代码:

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类:

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的数据,以便DatePicker pickerDate中的TableColumn <?,?> dateCol添加所选日期。

更新:到现在为止我把ComboBox的值放到一个名为text的String中,但如何从text中传入TableColumn eventCol这个值?

非常感谢!

1 个答案:

答案 0 :(得分:0)

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