2015-04-18 162 views
-1

我必须编写方法,它将获取我的字符串(显示以逗号分隔的小时),并返回字符串的ListProperty。 在我的构造函数中我有逗号分隔的字符串为ListProperty

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

我想用的方法从这个话题:https://stackoverflow.com/a/7488676/4750111

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

但它会创建的ArrayList。有没有像这样的功能,但对于ListProperty?

回答

1

你可以做

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

顺便说一句,你真的需要一个ListProperty?我从来没有找到它的用途;我发现只使用一个普通的ObservableList,注册听众就足够了。在参数上面SimpleListProperty,即

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

为您提供您所需要的元素进行观察的名单。

+0

谢谢,我不得不改变声明为'this.showingHour = new SimpleListProperty (FXCollections.observableList(new ArrayList ()));'然后做分割 – Paus

1

如何:

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

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

当我尝试这个,我'得到“java.lang.UnsupportedOperationException”.. – Paus

+0

从哪一行 - 添加或创建? – Kuba

+0

添加。我得到字符串从其他方法拆分“getText” – Paus