Comma separated String to ListProperty

550 Views Asked by At

I must write method, which will get my string(showing hours separated with commas), and return ListProperty of Strings. In my constructor I have

this.showingHour = new SimpleListProperty<String>();

I wanted to use method from this topic: https://stackoverflow.com/a/7488676/4750111

List<String> items = Arrays.asList(str.split("\\s*,\\s*"));

But it will create ArrayList. Is there function like this, but for ListProperty?

2

There are 2 best solutions below

1
James_D On BEST ANSWER

You can do

    ListProperty<String> list = new SimpleListProperty<(
        FXCollections.observableArrayList(str.split("\\s*,\\s*")));

As an aside, do you really need a ListProperty? I've never found a use for it; I find just using a regular ObservableList and registering listeners with it is enough. The arguments to SimpleListProperty above, i.e.

FXCollections.observableArrayList(str.split("\\s*,\\s*"))

gives you an observable list with the elements you need.

4
Kuba On

How about:

ListProperty<String> lp= new ListProperty<>();

lp.addAll(Arrays.asList(str.split("\\s*,\\s*")));