Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 743
sasiek Avatar asked Jan 14 '14 12:01

sasiek


People also ask

How to select a row in TableView JavaFX?

To select a row with a specific index you can use the select(int) method. Here is an example of selecting a single row with a given index in a JavaFX TableView: selectionModel. select(1);

How do I filter a TableView?

We can filter TableView content in two main ways – manually, or by using the FilteredList class JavaFX provides. In either case, we can update our search criteria by placing a ChangeListener on the search box TextField. This way, each time the user changes their search, the TableView is updated automatically.

What is TableView in JavaFX?

The TableView class provides built-in capabilities to sort data in columns. Users can alter the order of data by clicking column headers. The first click enables the ascending sorting order, the second click enables descending sorting order, and the third click disables sorting. By default, no sorting is applied.


1 Answers

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
                    }
                }
            });
like image 68
omgBob Avatar answered Oct 19 '22 11:10

omgBob