-1

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?

Community
  • 1
  • 1
czuponga
  • 3
  • 5

2 Answers2

1

How about:

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

lp.addAll(Arrays.asList(str.split("\\s*,\\s*")));
Kuba
  • 839
  • 7
  • 16
  • From which line - adding or creating? – Kuba Apr 18 '15 at 12:59
  • Adding. I'm getting String to split from other method by "getText" – czuponga Apr 18 '15 at 13:02
  • I see you have SimpleListProperty class. How it is declared? `class SimpleListProperty ` and then what? `extends` , `implements`? Paste whole line here. – Kuba Apr 18 '15 at 13:07
  • SimpleListProperty is declaration of this List, like other variables like SimpleStringProperty and SimpleIntegerProperty. It's all in one class Movie, where i just import javafx.beans.property.ListProperty and javafx.beans.property.SimpleListProperty – czuponga Apr 18 '15 at 13:32
1

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.

James_D
  • 201,275
  • 16
  • 291
  • 322
  • Thanks, I had to change declaration to `this.showingHour = new SimpleListProperty(FXCollections.observableList(new ArrayList()));` and then do the split – czuponga Apr 18 '15 at 14:47