2

This is the scenario:

I have a listView filled with 'Koe' objects, in the listView the 'Koe' objects are listed by their LabID property.

public class Koe {
private final SimpleIntegerProperty koeID = new SimpleIntegerProperty();
private final SimpleStringProperty labID = new SimpleStringProperty("");
...

//GETTERS
public int getKoeID(){
    return koeID.get();
}

public String getLabID(){
    return labID.get();
}
...

//SETTERS
public void setKoeID(int iKoeID) {
    koeID.set(iKoeID);
}

public void setLabID(String sLabID){
    labID.set(sLabID);
}
...

@Override
public String toString(){
    return "" + labID.get();
}

Now, once a Koe is selected, the Koe's data is loaded in another pane where users can edit it and press a Save button. When the Save button is pressed, the values from the TextFields are saved to the Koe object.

...
k.setLabID(txtLabID.getText());
...

The problem is however that when a user edited the LabID of a Koe object, the ListView does not show the new LabID in the ListView until the ListView being refreshed (As a test, I tried adding a dummy Koe object when users hit the Save button to force a refresh, then it seemed to work).

I did some reading about it and I understand that the ListView should be linked to a SimpleStringProperty object. But, when the users hit the Save button, the setLabID() method updates the Koe's LabID (which is a SimpleStringProperty object)? Does this has something to do with the toString() method? As this is being used to show the Koe's labID in the listView? But then again, this toString() method is linked to the labID SimpleStringProperty?

As you can see, i'm a bit confused. Although, everything works, it's just the problem with the refreshing of the ListView that I haven't been able to solve yet.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Perneel
  • 3,317
  • 7
  • 45
  • 66
  • [Can be helpful](http://stackoverflow.com/a/10914381/682495) – Uluk Biy Jul 16 '12 at 11:29
  • Hi Uluk, I added a SimplePropertyString getter for labid but still I don't get it how to connect that to the ListView? I understand how it works with TableView (TableColumn -> setCellValueFactory) but I do not find that method for a ListView? Any example code to manage that either in Java or FXML? – Perneel Jul 16 '12 at 11:59
  • See [this answer](http://stackoverflow.com/a/10700642/682495). Although that is about ComboBox, the same (cellFactory) can be applied to ListView as well. – Uluk Biy Jul 16 '12 at 12:11
  • You can think there is only one column in ListView so no need cellValueFactory or there is actually one implicitly. – Uluk Biy Jul 16 '12 at 12:21
  • I tried to add the cellFactory as you mentioned but it still doesn't work. The updateItem() method is not beeing fired when I press the 'Save' button to save the Koe's data. The values are stored in the Koe object in the ListView but the ListView (graphic) is not beeing updated. – Perneel Jul 16 '12 at 12:56
  • Ah wait, I found it! I had to place the setCellFactory code in the pressSaveButton method instead of the initialize method! – Perneel Jul 16 '12 at 13:01
  • No you have not to. setCellFactory is normally must be set only once in init method. Check your code again. – Uluk Biy Jul 16 '12 at 13:18
  • Then the updateItems() method from the CellFactory is not beeing fired... and still I don't see where I can connect the labid simpleStringProperty to the ListCell? – Perneel Jul 16 '12 at 13:30
  • The default cell factory uses koe.toString() to set the label of list item/cell. So the connection is via koe object as it uses labID in its toString method. If you customize the cell factory then you will see the values you have set in setText() and/or setGraphics() in updateitem() method as the label of list item/cell. Unfortunately the trick (that refreshes the updated item) used in TableView is not working for ListView. – Uluk Biy Jul 16 '12 at 14:27
  • So there is no sollution for this problem then? Except placing the cellFactory code at the Save button method? (which isn't ideal..) – Perneel Jul 16 '12 at 14:42
  • AFAIK no. There is a feature request for this and will be available in future releases of JavaFX. – Uluk Biy Jul 16 '12 at 14:54
  • Maybe handy for people who also want a manual refresh feature for ListView and TableView: http://javafx-jira.kenai.com/browse/RT-22599 – Perneel Jul 18 '12 at 10:56
  • @Perneel What is the workaround that you used? – Bhupen Nov 30 '12 at 21:54

0 Answers0