Bug in MFXTable when add button to Column

#392 · closed · 9 comments

View on GitHub ↗

NguyenVoXuanDuy123

Hi palexdev, I’m currently working on a JavaFX assignment for school and have been primarily using your library. Thank you for creating MaterialFX—it’s been incredibly helpful, especially the filtering feature in MFXTable. I wanted to add a button to a column in the table, and I came across this (https://github.com/palexdev/MaterialFX/issues/25). Following the instructions, I implemented the following code: ``` public static void addRentalAgreementActionColumns(MFXTableView<RentalAgreement> table) { MFXTableColumn<RentalAgreement> column = new MFXTableColumn<>("Action", true); column.setRowCellFactory(rentalAgreement -> { // Create a row cell MFXTableRowCell<RentalAgreement, String> rowCell = new MFXTableRowCell<>(p -> ""); // Create a button HBox buttonBox = new HBox(); MFXButton viewButton = new MFXButton(rentalAgreement.getId() + ""); viewButton.setOnMouseClicked(event -> { ScreenManager.openAsModal( ViewFactory.getRentalView(rentalAgreement), (Stage) viewButton.getScene().getWindow(), "Property Details" ); }); buttonBox.getChildren().add(viewButton); viewButton.setMaxWidth(50); viewButton.setMinWidth(50); buttonBox.setSpacing(5); rowCell.setGraphic(buttonBox); return rowCell; }); table.getTableColumns().add(column); } ``` The button is supposed to display the corresponding ID from rentalAgreement. However, when I scroll the table, the numbers displayed on the buttons always in range from 1 to 16 as video https://drive.google.com/file/d/1JaaRF7ci3KxydDn3afqlPYgy5NMeN9Lg/view?usp=sharing. Could you check this? Thank you so much!

Comments

NguyenVoXuanDuy123

Please rep me pls @palexdev :'( I need your help ASAP because the deadline of this assignment is on 12/1/2025.

palexdev

> Please rep me pls @palexdev :'( I need your help ASAP because the deadline of this assignment is on 12/1/2025. Ditch the old implementation and use the new table from [VirtualizedFX](https://github.com/palexdev/VirtualizedFX), you'll need Java 21 though Examples are in the [tests](https://github.com/palexdev/VirtualizedFX/blob/main/src/test/java/interactive/table/TableTestUtils.java#L295)

NguyenVoXuanDuy123

Thank you so much, but the new table doesn't have a filter feature, which is required in my assignment. :((

palexdev

> Thank you so much, but the new table doesn't have a filter feature, which is required in my assignment. :(( Ah, right Can't you just add it yourself? Like, wrap the table and a text field in a container (choose the layout you like) then use listen on the field's text to filter the table I got a very good example for a recent project of mine GUI: ![Screenshot 2025-01-10 130140](https://github.com/user-attachments/assets/f541aab6-8342-41c8-abb4-6ec36355cdc1) Code: [RefineList](https://github.com/palexdev/ArchitectFX/blob/f7428d5b03010dad40eccd1bbe3b48b5a42e6998/modules/frontend/src/main/java/io/github/palexdev/architectfx/frontend/utils/ui/RefineList.java): basically a wrapper for a source ObservableList, a FilteredList (what you need) and a SortedList. Makes it easy to filter and sort a source data set [TextField](https://github.com/palexdev/ArchitectFX/blob/f7428d5b03010dad40eccd1bbe3b48b5a42e6998/modules/frontend/src/main/java/io/github/palexdev/architectfx/frontend/views/content/ProjectsView.java#L143): I'm using a custom [TextField](https://github.com/palexdev/ArchitectFX/blob/f7428d5b03010dad40eccd1bbe3b48b5a42e6998/modules/frontend/src/main/java/io/github/palexdev/architectfx/frontend/components/TextField.java) just to easily add icons to it [Filter Action](https://github.com/palexdev/ArchitectFX/blob/f7428d5b03010dad40eccd1bbe3b48b5a42e6998/modules/frontend/src/main/java/io/github/palexdev/architectfx/frontend/views/content/ProjectsView.java#L274): from the previous link you can see I'm observing the field's text (`When.onInvalidated` is just a fancy way to add listeners to things from MFXCore), then I call this action but I don't filter immediately, I delay it with a PauseTransition of 200ms (optional, for a better UX) Let me condense everything in a few lines here: ```java // I'll use generics, replace with your model and cell classes RefineList<T> list = ...; VFXGrid<T, C> grid = new VFXGrid<>( list.getView(), // returns the filterend and sorted list, that's the one we want to use in the grid/table MyCell::new // Cell factory ); TextField field = new TextField("Filter"); field.textProperty().addListener(i -> filter(list, field.getText())) void filter(RefineList<T> list, String filter) { if (filter == null || filter.isEmpty()) { list.setPredicate(null); return; } Predicate<T> yourPredicate = ...; list.setPredicate(yourPredicate); } ```

NguyenVoXuanDuy123

thank you very much for the detailed instructions. But I am a freshman, I use JavaFX because the assignment requires me to use Javafx. I don't have much experience with Javafx or Java and the deadline is also coming soon. Maybe I have to find a different easier solution. By the way, thank you very much for the instructions and sorry for not being able to implement it.

palexdev

You have all the pieces, you just have to put them together, and it's easier than you may think 😉

NguyenVoXuanDuy123

as I understand, your solution supports only one kind of filter. I mean I want to be able to apply multiple filters for my table at one time which one is more complex.

stale[bot]

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot]

Closed for inactivity.