简体   繁体   English

如何从JavaFX中的TableView中删除CheckBox?

[英]How to delete a CheckBox from a TableView in JavaFX?

I'm writing a seating chart program using JavaFX. 我正在使用JavaFX编写座位表程序。 I have a table that keeps a list of students together that holds their name, grade, and whether they are present or absent (using a checkbox). 我有一张桌子,上面列出了学生名单,成绩,以及他们是否存在(使用复选框)。 I have a delete button that allows me to delete the students from the list. 我有一个删除按钮,允许我从列表中删除学生。 This works fine, however, whenever I delete the student object, the checkbox does not go along with it. 这很好,但是,每当我删除学生对象时,复选框都不会随之而来。 I'm not sure what I would need to add to get that to work. 我不确定我需要添加什么才能让它工作。 Here is a snippet of the delete code. 这是删除代码的片段。 There are also two images below that show my problem. 下面还有两张图片显示了我的问题。 This is my first post so please let me know if I missed something. 这是我的第一篇文章,如果我错过了什么,请告诉我。 Please help! 请帮忙! Thanks! 谢谢!

ObservableList<Student> items, sel;
items = currentTable.getItems();
sel = currentTable.getSelectionModel().getSelectedItems();
Student s = new Student("", "", 0, "");
for (Student p : sel) {
    items.remove(p);
    s = p;
}

Before Delete 删除前

[删除前

After Delete 删除后

[删除后

This has nothing to do with the delete or remove method. 这与deleteremove方法无关。 It has to do with what you did in TableColumn.setCellFactory() . 它与您在TableColumn.setCellFactory()所做的有关。

To get the checkbox you shown, you should have used (in general) one of the two methods: 要获得您显示的复选框,您应该使用(通常)两种方法之一:

Overriding updateItem() in TableCell while setting Cell Factory 在设置Cell Factory时覆盖TableCell中的updateItem()

There is this empty parameter in updateItem() which indicates whether the row is empty or not. updateItem()有一个empty参数,表示该行是否为空。 You need to use that to determine when not to show the checkbox. 您需要使用它来确定何时不显示该复选框。

column.setCellFactory(col -> {
    return new TableCell<Foo, Boolean>() {
        final CheckBox checkBox = new CheckBox();

        @Override
        public void updateItem(final Boolean selected, final boolean empty) {
            super.updateItem(selected, empty);

            if (!this.isEmpty()) {
                setGraphic(checkBox);
                setText("");
            }
            else {
                setGraphic(null); // Remove checkbox if row is empty
                setText("");
            }
        }
    }
}

Using CheckBoxTableCell 使用CheckBoxTableCell

JavaFX API has this convenient class CheckBoxTableCell that would do all these for you. JavaFX API有这个方便的类CheckBoxTableCell ,可以为你完成所有这些。 Most people find this class hard to use because there are 2 things that you need to ensure to use it correctly: 大多数人发现这个类很难使用,因为有两件事你需要确保正确使用它:

  1. The TableView that the column belongs to must be editable. 列所属的TableView必须是可编辑的。
  2. The TableColumn itself must be editable. TableColumn本身必须是可编辑的。

Example: 例:

tableView.setEditable(true);
tableColumnSelected.setCellFactory(CheckBoxTableCell.forTableColumn(tableColumnSelected));
tableColumnSelected.setEditable(true);

As for whether which entry you want to be removed with the delete button, you just need to remove the correct items from the TableView . 至于您希望使用删除按钮删除哪个条目,只需从TableView删除正确的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM