0

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Schwamm007
  • 77
  • 1
  • 6

1 Answers1

1

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.

Community
  • 1
  • 1
Itai
  • 6,641
  • 6
  • 27
  • 51
  • 1
    Hey, that's cool! Thank you for that fast reply! I was about to report about the converting fail, when you already augmented your answer! Thanks. – Schwamm007 Jan 14 '16 at 10:53