在选中该行和最后一列中的复选框时删除 TableViev 行

Delete a TableViev Row on selecting a checkbox in that row and in the last column

我正在尝试做到这一点,以便每当我 select 我的表格最后一列中的复选框时,我都会删除该行。我有一个任务对象,我输入到表视图中,其中包含相关字段字符串名称、字符串日期字符串和布尔值 tickOff。有人可以帮忙吗?

这是class:

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;
}

这是我的场景的控制器 class,tableviwew 所在的场景。

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);
    
}

使用 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);
        
    }
}

要允许将 ActionEvent 分配给 CheckBoxTableCell,请创建您自己的 Class 名为 CheckBoxTableCell,如下所示:

    private ObservableValue<T> ov;
    private TableView<Task> tableView; //The table that will house the checkbox.

    public CheckBoxTableCell() {

        CheckBox checkBox = new CheckBox();
        this.checkBox = checkBox;

        checkBox.selectedProperty().addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                if (newValue) {
                    // getTableView().getSelectionModel().select( getIndex() );
                    System.out.println("Xd");
                } else {
                    // getTableView().getSelectionModel().clearSelection( getIndex() );
                    System.out.println("Nothing");
                }
            }
        });

        setGraphic(checkBox);

    }

    @Override
    public void updateItem(T item, boolean empty) {
        // System.out.println("updateItem");
        super.updateItem(item, empty);

        if (empty) {
            setText(null);
            setGraphic(null);

        } else {

            setGraphic(checkBox);

            if (ov instanceof BooleanProperty) {

                checkBox.selectedProperty().unbindBidirectional((BooleanProperty) ov);

            }

            ov = getTableColumn().getCellObservableValue(getIndex());
            if (ov instanceof BooleanProperty) {

                checkBox.selectedProperty().bindBidirectional((BooleanProperty) ov);

            }

        }

    }

    void setTableView(TableView<Task> tableView) {
        this.tableView = tableView;
    }

    void setTableColumn(TableColumn<Task, Boolean> tableColumn) {
        this.tableColumn = tableColumn;
    }
}