本文介绍了Java ListChangeListener WasUpdated()不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在observableList中修改一个名为EventShowable的对象时触发一些代码。
这是我的代码。

  mainApp.getCalendars()。get(i).getListEvents()。addListener( new ListChangeListener< EventShowable>(){
@Override
public void onChanged(ListChangeListener.Change<?extends EventShowable> c){
while(c.next()){
if(c.wasUpdated()){
//这不起作用。
//执行更新

}
if(c.wasAdded()) {
//执行某事}

wasAdded()执行得很好但不是wasUpdate( )。
如何在修改EventShowable时获得有用的东西?谢谢



PS:在JavaDoc中:
public boolean wasUpdated()
表示getFrom()(包括)与getTo()独占之间的元素已更改。 Th is是唯一可选的事件类型,并且可能不会被所有ObservableLists触发。



你还没有给出足够的细节来给出完整的答案,但如果你的 EventShowable 定义了诸如

  public class EventShowable {
public IntegerProperty xProperty(){...}
public StringProperty yProperty(){。 ..}
// ...
}

然后创建一个触发更新的列表e当 x y 更改时,通风口

  ObservableList< EventShowable> listEvents = 
FXCollections.observableArrayList(eventShowable - >
new Observable [] {eventShowable.xProperty(),eventShowable.yProperty()});


I want to trigger some code when a object called EventShowable have been modified in an observableList.Here is my code.

                mainApp.getCalendars().get(i).getListEvents().addListener(new ListChangeListener<EventShowable>() {
                @Override
                public void onChanged(ListChangeListener.Change<? extends EventShowable> c) {
                    while (c.next()) {
                        if (c.wasUpdated()) {
                          //this doesn't work.
                          //perform updated

                        }
                        if (c.wasAdded()){
                            //perform something }

The wasAdded() perform well but not the wasUpdate().How to get something that works when a EventShowable have been modified ? Thanks

P.S : in the JavaDoc :public boolean wasUpdated()Indicates that the elements between getFrom() (inclusive) to getTo() exclusive has changed. This is the only optional event type and may not be fired by all ObservableLists.https://docs.oracle.com/javase/8/javafx/api/javafx/collections/ListChangeListener.Change.html#wasUpdated--

解决方案

Create your list using an extractor.

You haven't really given enough detail to give a complete answer, but if your EventShowable defines properties such as

public class EventShowable {
    public IntegerProperty xProperty() { ... }
    public StringProperty yProperty() { ... }
    // ...
}

then to create a list that fires update events when x or y change you do

ObservableList<EventShowable> listEvents =
    FXCollections.observableArrayList(eventShowable ->
        new Observable[] { eventShowable.xProperty(), eventShowable.yProperty() });

这篇关于Java ListChangeListener WasUpdated()不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 16:00