How to monitor changes on objects contained in an ObservableList JavaFX

前端 未结 2 1309
死守一世寂寞
死守一世寂寞 2020-12-17 16:58

I really have difficulties to understand how the ObservableList object is working in JavaFX. I want to monitor if an object in the List has been modified. So fa

2条回答
  •  情书的邮戳
    2020-12-17 17:33

    public static void main(String[] args) {
        ArrayList intList = new ArrayList();
        intList.add(0);
        intList.add(1);
    
        ObservableList ob = FXCollections.observableArrayList(intList);
        ob.addListener(new ListChangeListener() {
            @Override
            public void onChanged(javafx.collections.ListChangeListener.Change c) {
                System.out.println("Changed on " + c);
                if(c.next()){
                    System.out.println(c.getFrom());
                }
    
            }
    
        });
    
        ob.set(0, 1);
    }
    

    The event (c in my case) is the index that the change occurred on (when you do .getFrom()). Also if you print the event out you get a string that tells you exactly what happend. You mistakenly interpret it has a change being done on the list as a whole!

提交回复
热议问题