Waluigi
Waluigi

Reputation: 67

how can i on update item of tableview?

i have override the update item of TableCell in javafx 8 but when i scroll the items of tableview ,some items will be updated randomly

Column.setCellFactory(new Callback<TableColumn<Person, Boolean>, TableCell<Person, Boolean>>() {
            @Override
            public TableCell<Person, Boolean> call(TableColumn<Person, Boolean> param) {
                return new TableCell<Person, Boolean>() {
                    @Override
                    public void updateItem(Boolean item, boolean empty) {
                        super.updateItem(item, empty);
                        if (!isEmpty()) {
                            if (item.booleanValue()) {
                                setGraphic(new ImageView(IconResource.getImage(ICON.FLAG)));
                            }
                        } else {
                            setGraphic(null);
                        }
                    }
                };
            }
        });

Upvotes: 0

Views: 112

Answers (1)

fabian
fabian

Reputation: 82451

You need to make sure that the graphic property of the cell is updated properly regardless of the item (or it the fact that it's "missing").

In your case you do not properly handle cells that go from true as item to false as item: You simply keep the graphic for true instead of removing it.

Furthermore I recommend keeping a reference to the ImageView to avoid recreating it:

Column.setCellFactory(new Callback<TableColumn<Person, Boolean>, TableCell<Spectrum, Boolean>>() {


    @Override
    public TableCell<Person, Boolean> call(TableColumn<Person, Boolean> param) {
        return new TableCell<Person, Boolean>() {

            private final ImageView image = new ImageView(IconResource.getImage(ICON.FLAG));

            @Override
            public void updateItem(Boolean item, boolean empty) {
                super.updateItem(item, empty);
                setGraphic(item == null || !item ? null : image);
            }
        };
    }
});

Upvotes: 2

Related Questions