S
S
Stoned Shaman2015-09-10 21:50:30
Java
Stoned Shaman, 2015-09-10 21:50:30

JavaFX. How to make automatic updating of ListView, after operation of change of record in it?

Hello.
I write applications in javafx. Faced the following problem, and I can not understand what I'm doing wrong.
For data storage I use

ObservableList<User> users = new FXCollections.observableArrayList();

I initialize in the initialize () method of the controller, displaying data from the User class.
// Controller
...
@FXML
private void initialize() {
    nameList.setCellFactory((ListView<User> userListView) -> new UsersFormatCell());
}
...

UserFormatCell class (purely licked from java-demo):
public class UsersFormatCell extends ListCell<User> {
    @Override
    public void updateItem(User user, boolean empty) {
        super.updateItem(user, empty);

        // format of the cell in list

        setText(user == null ? "" : user.getName());
    }
}

The User class has StringProperty fields (specifically SimpleStringProperty), and a corresponding set of getters and setters, i.e. can return both String and StringProperty values, and setting the values ​​goes through StringProperty.
In theory, using ObservableList makes it possible to notify GUI elements about changes if the fields of the model class use the StringProperty interface. And this is true, if you use TableView, then for any operations (adding, changing, deleting) with the table, the table is updated automatically. However, this does not happen in the case of ListView. The list is updated in the GUI, only after the addition and removal operation, and nothing happens when the change occurs (i.e. the value itself in the ObservableList changes, but this is not visible in the GUI until a new entry is added or the old one is deleted).
Please tell me what I missed or did wrong.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Andreevich, 2016-01-11
@SibWol

If I understand your question correctly, then you should try to re-register nameList.setCellFactory() in the method that makes changes to the ListView; same as in initialize

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question