2015-04-18 160 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