palexdev / MaterialFX

A library of material components for JavaFX
GNU Lesser General Public License v3.0
1.21k stars 122 forks source link

Trying to add button in MFXPaginatedtableView, can't retrieve index #368

Closed gix85 closed 9 months ago

gix85 commented 9 months ago

Hi all, I really like your library, thanks for all the work. I'm struggling to add a button to my paginated table view, in plain javafx i would usually do something like that:

Callback<TableColumn<Person,` String>, TableCell<Person, String>> cellFactory = new Callback<TableColumn<Person, String>, TableCell<Person, String>>() { @Override public TableCell call(final TableColumn<Person, String> param) { final TableCell<Person, String> cell = new TableCell<Person, String>() {

                final Button btn = new Button("Just Do It");

                @Override
                public void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);
                    if (empty) {
                        setGraphic(null);
                        setText(null);
                    } else {
                        btn.setOnAction(event -> {
                            Person person = getTableView().getItems().get(getIndex());
                            System.out.println(person.getFirstName()
                                    + "   " + person.getLastName());
                        });
                        setGraphic(btn);
                        setText(null);
                    }
                }
            };
            return cell;
        }
    };

I adapted this code to a MFXPaginatedTableView but rowcell and column lack getTableView and getIndex method. I can pass the tableview from outside but i can't think of a way to determine the index of the row in order to get my cell object. I didn't find a way in the documentation (or probably simply miss it) to solve my issue. There's maybe a simpler way to achieve what i'm trying to do?

palexdev commented 9 months ago

I suggest you to use VirtualizedFX instead. It's easier (given that you read the documentation!) to use and better documented.