Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Adding Invalidation and Change listeners to a Listproperty

Kishori SharanSep 13 2012 — edited Sep 13 2012
I am playing with adding the following three types of listeners to a ListProperty:
1. InvalidationListener
2. ChangeListener
3. ListChangeListener

When I add only an InvalidationListener and a ChangeListener, change events are not fired; only invalidation events are fired. When I add the third listener, ListChangeListener, all three events are fired. I am using JavaFX 2.2 with Java 8 Preview b50. A combination of 2nd and 3rd listeners works fine. Is it a bug, or am I missing something? My code is as follows.

// ListPropertyTest.java
package test;

import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.property.ListProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.value.ObservableValue;

import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;


public class ListPropertyTest {
public static void main(String[] args) {
// Create an observable list
ListProperty<String> lp =
new SimpleListProperty<String>(FXCollections.observableArrayList());

// Add invalidation, change, and List change listeners
lp.addListener(ListPropertyTest::invalidated);
lp.addListener(ListPropertyTest::changed);
//lp.addListener(ListPropertyTest::onChanged);

System.out.println("Before add()");
lp.add("one") ;
System.out.println("After add()");

System.out.println("\nBefore set()");
// Change the list itself
lp.set(FXCollections.observableArrayList("two", "three"));
System.out.println("After set()");
}

public static void invalidated(Observable list) {
System.out.println("List property is invalid.");
}

public static void changed(ObservableValue<? extends ObservableList<String>> observable,
ObservableList<String> oldList,
ObservableList<String> newList ) {
System.out.print("List Property has changed.");
System.out.print(" Old List: " + oldList);
System.out.println(", New List: " + newList);
}

public static void onChanged(ListChangeListener.Change<? extends String> change) {
while (change.next()) {
String action =
change.wasPermutated() ? "Permutated":
change.wasUpdated() ? "Updated":
change.wasRemoved() && change.wasAdded() ? "Replaced":
change.wasRemoved() ? "Removed": "Added";

System.out.print("Action taken on the list: " + action);
System.out.print(", Removedt: " + change.getRemoved());
System.out.println(", Added: " + change.getAddedSubList());
}
}
}

Edited by: user6588303 on Sep 13, 2012 11:12 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 11 2012
Added on Sep 13 2012
2 comments
1,050 views