所以我有以下代码:
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 bttnAddEvent
在ComboBox
中添加TableColumn <?,?> eventCol
中的选定数据。除此之外,另一个挑战是同时使用相同的Button bttnAddEvent
来自ComboBox
的数据,以便DatePicker pickerDate
中的TableColumn <?,?> dateCol
添加所选日期。
更新:到现在为止我把ComboBox的值放到一个名为text的String中,但如何从text中传入TableColumn eventCol这个值?
非常感谢!
答案 0 :(得分:0)
我找到了解决方案,并为满足此需求的人更新了代码。