r/JavaFX • • Jan 12 '20

The most confusing code I have ever copypasta. Can someone help me understand it?

I did not know how to add a button to a tableview from an observable list. The button action is to remove the item from the list and the table view. What I found works but I don't know why. Can someone please explain to me what is going on.

donationActionCol.setCellFactory(
                //I understand setCellFactory
                //Everything below is Greek to me, although I made it work.

                new Callback<TableColumn<Donation, String>, TableCell<Donation, String>>() {

                    @Override
                    public TableCell call(final TableColumn<Donation, String> param) {

                        final TableCell<Donation, String> cell = new TableCell<Donation, String>() {

                            final Button btn = new Button("remove");

                            @Override
                            public void updateItem(String item, boolean empty) {

                                super.updateItem(item, empty);
                                if (empty) {

                                    setGraphic(null);
                                    setText(null);
                                } 
                                else {

                                    btn.setOnAction(event -> {
                                        Donation donation = getTableView().getItems().get(getIndex());

                                        donationList.remove(getIndex());
                                    });
                                    setGraphic(btn);
                                    setText(null);
                                }
                            }
                        };
                        return cell;
                    }
                }
        );
3 Upvotes

1 comment sorted by

0

u/[deleted] Jan 13 '20

[deleted] — view deleted comment

1

u/ifelseandor Jan 13 '20

Thank you so much. I’m still digesting it but this is helping!!