wieczors
wieczors

Reputation: 351

javafx tableview detect any change upon any of rows including addition of new one

because I don't really know the sollution. Lets say i have a TableView that holds info about product: description, quantity and the price. Also have a label that shows a sum of prices (times quantity) of all products from a table. Now i want to be notified about any changes that took affect on this component like changes in existing rows or addition of a new one. Is there any listener or sth similar to achieve it?

Upvotes: 5

Views: 16065

Answers (1)

omgBob
omgBob

Reputation: 527

Usually you construct a TableView by passing an ObservableList to it. Something like this: TableView myTable = new TableView<>(myObservableList);

ObservableList<ClassOfTheObjectsInTheList> myObservableList = FXCollections.FXCollections.observableArrayList(anyNoneObservableCollection);
TableView<ClassOfTheObjectsInTheList> myTable = new TableView<>(myObservableList);

You can add a ListChangeListener to any ObservableList at will by doing:

myObservableList.addListener(new ListChangeListener<ClassOfObjectsInTheList>(){

                @Override
                public void onChanged(javafx.collections.ListChangeListener.Change<? extends ClassOfObjectsInTheList> pChange) {
                    while(pChange.next()) {
                        // Do your changes here
                    }
                }
            });

Upvotes: 15

Related Questions