VANILKA
VANILKA

Reputation: 684

JavaFX row in table view didn't remove ater remove element

I have a litre problem with table view. When I remove element from observable list, the row correspandont on this element is not deleted.

I have a relations list ( person1, type, person2 => children) When I add a new child the rellation is created (null, neutral, null => child).

After add some people, I change the relations beetwen them, so, when I indicate the siblings, some realtions are deleted. But it is still visible in table view. It is not selectable, but when I click on, it is indique . the last relation in the list.

When I add new Person, the row are overrided.

this.relationSimLeftColumn.setCellValueFactory(cellData -> cellData.getValue().simLeftProperty());
this.relationSimRightColumn.setCellValueFactory(cellData -> cellData.getValue().simRightProperty());
this.relationTypeColumn.setCellValueFactory(cellData -> cellData.getValue().typeProperty());

and cell factory :

 private Callback<TableColumn<GTX_Relation, GTX_Member>,
            TableCell<GTX_Relation, GTX_Member>> setMemberCellFactory(String parameter) {
        Callback<TableColumn<GTX_Relation, GTX_Member>, TableCell<GTX_Relation, GTX_Member>> callback =
                new Callback<TableColumn<GTX_Relation, GTX_Member>, TableCell<GTX_Relation, GTX_Member>>() {
                    @Override
                    public TableCell<GTX_Relation, GTX_Member> call(TableColumn<GTX_Relation, GTX_Member> param) {
                        TableCell<GTX_Relation, GTX_Member> cell = new TableCell<GTX_Relation, GTX_Member>() {

                            @Override
                            protected void updateItem(GTX_Member item, boolean empty) {
                                super.updateItem(item, empty);
                                ImageView imageview = new ImageView();
                                if (item != null) {
                                    imageview.setFitHeight(TABLE_IMAGE_MEMBER_HEIGHT);
                                    imageview.setFitWidth(TABLE_IMAGE_MEMBER_WIDTH);
                                    imageview.setImage(new Image(item.getPhoto()));
                                    setGraphic(imageview);
                                } else {
                                    if (!empty) {
                                        imageview.setFitHeight(TABLE_IMAGE_MEMBER_HEIGHT);
                                        imageview.setFitWidth(TABLE_IMAGE_MEMBER_WIDTH);
                                        String path = parameter.equals("LEFT") == true ?
                                                ImageFiles.NO_NAME_FEMALE.toString() : ImageFiles.NO_NAME_MALE.toString();
                                        imageview.setImage(new Image(path));
                                        setGraphic(imageview);
                                    }
                                }
                            }

                        };
                        return cell;
                    }
                };

        return callback;
    }

Upvotes: 1

Views: 142

Answers (1)

James_D
James_D

Reputation: 209553

Your cell factory needs to handle the case where the cell is empty.

When you remove an item from the table's items list, a cell that was previously used to display an item will (potentially) be reused as an empty cell; i.e. its updateItem(null, true) method will be called. Your current implementation doesn't do anything for the case where item == null && empty == true, so the cell's appearance won't be changed.

You need

@Override
protected void updateItem(GTX_Member item, boolean empty) {
    super.updateItem(item, empty);
    ImageView imageview = new ImageView();
    if (item != null) {
        imageview.setFitHeight(TABLE_IMAGE_MEMBER_HEIGHT);
        imageview.setFitWidth(TABLE_IMAGE_MEMBER_WIDTH);
        imageview.setImage(new Image(item.getPhoto()));
        setGraphic(imageview);
    } else {

        if (empty) {

            setGraphic(null);

        } else {

            imageview.setFitHeight(TABLE_IMAGE_MEMBER_HEIGHT);
            imageview.setFitWidth(TABLE_IMAGE_MEMBER_WIDTH);
            String path = parameter.equals("LEFT") == true ?
                    ImageFiles.NO_NAME_FEMALE.toString() : ImageFiles.NO_NAME_MALE.toString();
            imageview.setImage(new Image(path));
            setGraphic(imageview);
        }
    }
}

Upvotes: 2

Related Questions