我有这些项目:
@FXML
private Button bttnRemove;
@FXML
private TableView<EventsBean> eventsTable;
@FXML
private TableColumn<EventsBean, String> eventCol;
@FXML
private TableColumn<EventsBean, LocalDate> dateCol;
@FXML
private TableColumn<EventsBean, Boolean> doneCol;
@FXML
private TableColumn<EventsBean, String> observationCol;
@FXML
private TableColumn<EventsBean, Boolean> removeCol;
并在removeCol中创建复选框:
ObservableList<EventsBean> dataList = FXCollections.observableArrayList();
@Override
public void initialize(URL location, ResourceBundle resources) {
removeCol.setCellFactory(CheckBoxTableCell.forTableColumn(removeCol));
removeCol.setCellValueFactory(new PropertyValueFactory<EventsBean, Boolean>("remove"));
eventsTable.setItems(dataList);
bttnAddEvent.setOnAction((ActionEvent e) -> {
text = eventsSelector.getValue().toString();
dataList.add(new EventsBean(text, isoDate, ""));
});
bttnRemove.setOnAction((ActionEvent e) -> {
//code here...
});
}
我想使用bttnRemove删除从removeCol检查复选框的行。
答案 0 :(得分:0)
我发现下一个解决方案非常有效:
public class EventsBean {
private SimpleStringProperty event;
private SimpleObjectProperty<LocalDate> date;
private SimpleStringProperty observation;
private SimpleBooleanProperty selected;
public EventsBean(String event, LocalDate date, String observation, boolean selected) {
this.event = new SimpleStringProperty(event);
this.date = new SimpleObjectProperty<LocalDate>(date);
this.observation = new SimpleStringProperty(observation);
this.selected = new SimpleBooleanProperty(selected);
}
// the other getters and setters
public boolean getSelected(){
return selectedProperty().get();
}
public SimpleBooleanProperty selectedProperty() {
return this.selected;
}
}
在我的Controller类中初始化方法:
removeCol.setCellValueFactory(cellData -> cellData.getValue().selectedProperty());
bttnRemove.setOnAction((ActionEvent e) -> {
ObservableList<EventsBean> dataListToRemove = FXCollections.observableArrayList();
for (EventsBean bean : dataList) {
if (bean.getSelected()) {
dataListToRemove.add(bean);
}
}
dataList.removeAll(dataListToRemove);