当我选中复选框时,如何从 Table 视图中删除行?
When I have Check Box checked how to delete row from Table View?
我有这些物品:
@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 中删除选中复选框的行。
我发现下一个非常有效的解决方案:
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;
}
}
在我的控制器class中初始化方法:
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);
我有这些物品:
@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 中删除选中复选框的行。
我发现下一个非常有效的解决方案:
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;
}
}
在我的控制器class中初始化方法:
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);