Schwamm007
Schwamm007

Reputation: 77

JavaFX column/cell-value not updating

I recently started programming an application with JavaFX (jdk 1.8.0_66). I got a few table(view)s, one of them should be an overview about all the 'subscription'-objects. Therefore I create the tableview and populate it with an observable list:

TableView subsTable = new TableView(SubscriptionAdmin.getObservableSubList());

My table for example kinda looks like this:

Subscription | Participants  
Netflix      | 4  
Whatever     | 8
TableColumn<Subscription,String> nameCol = new TableColumn("Subscription");
nameCol.setCellValueFactory(new PropertyValueFactory("name"));
TableColumn<Subscription, Integer> partCol = new TableColumn("Participants");
partCol.setCellValueFactory(
                cellData -> new ReadOnlyObjectWrapper<>(cellData.getValue().getParticipants().size())
);

Now whenever I add an participant to my list, the number in the according cell should be increased by one but it won't - except I restart the application. Hope someone of you can help me / explain me what's the problem.

Upvotes: 0

Views: 641

Answers (1)

Itai
Itai

Reputation: 6911

The problem is the table (or rather - the cell) has no way of knowing the data changed. If getParticipants returns an ObservableList the best solution would be to create a binding to its' size -

partCol.setCellValueFactory(
            cellData -> Bindings.size(cellData.getValue().getParticipants())
);

If it is not an ObservableList you may consider changing it to be one (after all - you wish to update the display when it changes). Otherwise you will have to manually update the table whenever you change the size of the list.

Edit: You may need to either add .asObject() to the binding call, or else change the type of the column to Number and not Integer. See this question for discussion.

Upvotes: 1

Related Questions