CodexBloom - Programming Q&A Platform

JavaFX TableView Not Updating with ObservableList After Data Change in Controller

👀 Views: 40 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-14
java javafx observablelist Java

I'm trying to debug I'm experimenting with I've looked through the documentation and I'm still confused about I'm dealing with I'm facing an issue where my JavaFX `TableView` does not update when the underlying `ObservableList` is modified from the controller. The `TableView` is bound to an `ObservableList<Person>` that I populate with some initial data. However, after adding a new `Person` object to the list, the UI does not reflect this change until I manually refresh the table. Here's the relevant part of my code: ```java import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.scene.control.TableView; public class PersonController { private TableView<Person> tableView; private ObservableList<Person> personList; public PersonController(TableView<Person> tableView) { this.tableView = tableView; this.personList = FXCollections.observableArrayList(); this.tableView.setItems(personList); loadInitialData(); } private void loadInitialData() { personList.add(new Person("John", "Doe")); personList.add(new Person("Jane", "Smith")); } public void addPerson(String firstName, String lastName) { personList.add(new Person(firstName, lastName)); // UI does not refresh here } } ``` I expected the `TableView` to automatically update once I add a new `Person`, but it remains unchanged. I've tried calling `tableView.refresh()` after modifying the list, but that doesn't seem to help. I also verified that the `ObservableList` is not a copy and is the same reference that the `TableView` is bound to. I'm using JavaFX 17 and JDK 17. Is there a common pitfall I might be missing, or is there a specific way to ensure that changes to the `ObservableList` trigger an update in the `TableView`? My development environment is CentOS. Is this even possible? The project is a REST API built with Java. Thanks for your help in advance! Am I approaching this the right way? Am I missing something obvious?