Issue
I am attempting to make it so that whenever I select a checkbox in the last column of my tableviwew, I delete said row. I have a Task object I input into the tableview with relevant fields String name, String dateString and boolean tickOff. Can someone help?
public class Task {
private BooleanProperty tickOff;
private String name;
private String description;
private String dateString;
private String reminder1, reminder2, reminder3, reminder4, reminder5, reminder6;
private int year, month, day;
public Task(String name, String description, int year, int month, int day, boolean ticoff) {
this.name = name;
this.description = description;
this.year = year;
this.month = month;
this.day = day;
dateString = day + "/" + month + "/" + year;
this.tickOff = new SimpleBooleanProperty(ticoff);
// this.tickOff = new SimpleBooleanProperty(false);
/*
* this.tickOff.addListener(new ChangeListener<Boolean>() { public void
* changed(ObservableValue<? extends Boolean> ov, Boolean t, Boolean t1) {
*
* System.out.println("Hi");
*
* } });
*/
}
/////////////
public boolean getSelected() {
return selectedProperty().get();
}
public BooleanProperty selectedProperty() {
return this.tickOff;
}
public boolean isChecked() {
return this.tickOff.get();
}
public boolean getickOff() {
return this.tickOff.get();
}
public BooleanProperty getTickOff() {
return this.tickOff;
}
public BooleanProperty checkedProperty() {
return this.tickOff;
}
This is the Controller class of my scene where the tableviwew resides.
public class ClassPlanController {
private TableView<Task> tasks;
@FXML
private TableColumn<Task, String> task;
@FXML
private TableColumn<Task, String> date;
@FXML
private TableColumn<Task, Boolean> done;
public void addTaskToList(Task taskName) {
task.setCellValueFactory(new PropertyValueFactory<>("name"));
date.setCellValueFactory(new PropertyValueFactory<>("dateString"));
done.setCellValueFactory(new PropertyValueFactory<>("tickOff"));
/*
done.setCellFactory(CheckBoxTableCell.forTableColumn(new Callback<Integer, ObservableValue<Boolean>>() {
public ObservableValue<Boolean> call(Integer param) {
return tasks.getItems().get(param).checkedProperty();
}
}));
/*
done.setCellFactory(new Callback<TableColumn<Task, Boolean>, TableCell<Task, Boolean>>() {
public TableCell<Task, Boolean> call(TableColumn<Task, Boolean> p) {
return new CheckBoxTableCell<Task, Boolean>();
}
});
done.setCellFactory(CheckBoxTableCell.forTableColumn(new Callback<Integer, ObservableValue<Boolean>>() {
public ObservableValue<Boolean> call(Integer param) {
return tasks.get(param).checkedProperty();
}
}));
*/
tasks.getItems().add(taskName);
tasks.setEditable(true);
}
Solution
Use a FilteredList
:
public class ClassPlanController {
private TableView<Task> tasks;
@FXML
private TableColumn<Task, String> task;
@FXML
private TableColumn<Task, String> date;
@FXML
private TableColumn<Task, Boolean> done;
private final ObservableList<Task> taskList = FXCollections.observableArrayList<>(task -> new Observable[] {task.checkedProperty()});
public void initialize() {
task.setCellValueFactory(new PropertyValueFactory<>("name"));
date.setCellValueFactory(new PropertyValueFactory<>("dateString"));
done.setCellValueFactory(cellData -> cellData.getValue().checkedProperty());
done.setCellFactory(column -> new CheckBoxTableCell(index -> tasks.getItems().get(index).checkedProperty()));
tasks.setItems(new FilteredList(taskList, task -> ! task.checkedProperty().get()));
}
public void addTaskToList(Task taskName) {
taskList.add(taskName);
}
}
Answered By - James_D
Answer Checked By - David Marino (JavaFixing Volunteer)