Is there a CHANGEABLE ListProperty in JavaFX?

678 views Asked by At

Is there a CHANGEABLE ListProperty in JavaFX? I need a ListProperty supprting add() and remove() methods, but I couldnt't find one. SimpleListProperty will throw UnsupportedOperationException.

All I found is to use the initializer to set the contents, but that's not what I need. I need to start with an empty Property end buld from there.

I cannot believe there's no such a thing; I must be blind, somehow. Can someone point me in the right direction, please?

2

There are 2 answers

0
ZioByte On BEST ANSWER

It turns out ListProperty() is an empty wrapper.
It does not initialize the wrapped ObservableList<>.
This is absolutely unclean from documentation and the UnsupportedOperationException thrown is misleading (should be a NPE).
Default constructor is useless in normal usage, use something like:

ListProperty<String> xxx = new SimpleListProperty<>(FXCollections.observableArrayList());

NOTE: Using a simple ObservableList does not work for me because I'm using Property methods (e.g.: getBean())

1
AJJ On

Observable list is the one you are looking for. It has add and remove methods.

import javafx.collections.ObservableList;

ObservableList<String> list = FXCollections.observableArrayList();
list.add("Jaya");
list.add("Prasad");
list.add("test");
list.remove(list.indexOf("test"));