[英]How do i properly move multiple items in a single listView control to another listveiw control in javaFx?
[英]Items changed event for JavaFX ListView control
我正在尋找類似JavaFX ListView
控件的Item Changed Event
或Item Count Changed Event
類的東西,或者對於任何集合類型控件來說,通常來說就是這樣。
因為我有一些Button,所以僅當ListView中至少有一項時才希望啟用,否則Button應該處於禁用狀態。
我猜測可能是將ChangeListener
添加到ListView
控件。 那是正確的方法嗎?
任何建議我們如何實現這一目標。
JavaFX Listview
提供了帶有簽名的方法
public final ObservableList<T> getItems()
您可以將偵聽器添加到可觀察列表,只要將項目添加到ListView
或從ListView
刪除,就會調用該偵聽器。
aListView.getItems().addListener(new ListChangeListener() {
@Override
public void onChanged(ListChangeListener.Change change) {
System.out.println("Detected a change! ");
}
});
其他“集合”控件也提供了類似的功能。
當有人調用ListView#setItems
並完全更改可觀察列表時,這也將起作用:
InvalidationListener updateScrollBarListener = ...
aListView.itemsProperty().addListener((obs, old, current) -> {
if(old != null) {
old.removeListener(updateScrollBarListener);
}
if(current != null) {
current.addListener(updateScrollBarListener);
}
});
我一直在尋找一種使它更短的方法(也許使用Bindings
),但是到目前為止還沒有找到。
使用Bindings.isEmpty
基於ListView
項添加Bindings.isEmpty
button.disableProperty().bind(Bindings.isEmpty(listView.getItems()));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.