Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8090975

[Base] Provide a FXCollections.swap method

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • 8u25
    • javafx
    • None

      The javadoc of FXCollections states that it is a "Utility class that consists of static methods that are 1:1 copies of java.util.Collections methods". But it doesn't have a FXCollections.swap(ObservableList<T>, int, int) counterpart of Collections.swap(List<T>, int, int).

      The problem with using Collections.swap on an ObservableList is that it fires ListChangeListener multiple times. For example run the following code:

      import java.util.Collections;
      import java.util.ArrayList;

      import javafx.collections.ObservableList;
      import javafx.collections.ListChangeListener;
      import javafx.collections.FXCollections;
       
      public class CollectionsSwapDemo {
       
          public static void main(String[] args) {
       
              // Use Java Collections to create ArrayList
              ArrayList<String> list = new ArrayList<>();
              list.add("d");
              list.add("b");
              list.add("a");
              list.add("c");
              
              // Now add observability by wrapping it with ObservableList
              ObservableList<String> observableList = FXCollections.observableList(list);
              
              observableList.addListener(new ListChangeListener<String>() {
                  @Override
                  public void onChanged(ListChangeListener.Change change) {
                     System.out.println("Change Detected");
                  }
              });
              
              Collections.swap(observableList, 0, 2);
          }
      }


      Observed output: Change Detected is printed two times.

      It will be nice to have an FXCollections.swap method which limits the number of notifications.

            Unassigned Unassigned
            asarkar Anirvan Sarkar
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Imported: