JavaFX集合


JavaFX集合

JavaFX中的集合由javafx.collections包定義,javafx.collections包由以下介面和類組成:

介面

介面 描述
ObservableList 允許跟蹤更改的列表
ListChangeListener 接收更改通知的介面
ObservableMap 允許跟蹤更改的對映
MapChangeListener ObservableMap接收更改通知的介面

描述
FXCollections 實用程式類對映到java.util.Collections
ListChangeListener.Change 表示對ObservableList所做的更改
MapChangeListener.Change 表示對ObservableMap所做的更改

範例

以下程式碼顯示了如何使用ObservableListObservableMapFXCollections

import java.util.ArrayList;
import java.util.List;

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

public class Main {

    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();

        ObservableList<String> observableList = FXCollections.observableList(list);
        observableList.addListener(new ListChangeListener() {
            @Override
            public void onChanged(ListChangeListener.Change change) {
                System.out.println("有修改操作!");
            }
        });
        observableList.add("item one");
        list.add("item two");
        System.out.println("Size: " + observableList.size());

    }
}

上面的程式碼生成以下結果。

有修改操作!
Size: 2

範例-2

以下程式碼顯示了如何監聽ObservableMap上的更改。

import java.util.HashMap;
import java.util.Map;

import javafx.collections.FXCollections;
import javafx.collections.MapChangeListener;
import javafx.collections.ObservableMap;

public class Main {

  public static void main(String[] args) {
    Map<String, String> map = new HashMap<String, String>();
    ObservableMap<String, String> observableMap = FXCollections
        .observableMap(map);
    observableMap.addListener(new MapChangeListener() {
      @Override
      public void onChanged(MapChangeListener.Change change) {
        System.out.println("change! ");
      }
    });
    observableMap.put("key 1", "value 1");
    map.put("key 2", "value 2");

  }
}

上面的程式碼生成以下結果。

change!

範例-3

以下程式碼顯示了如何找出已更改的內容。

import java.util.ArrayList;
import java.util.List;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;

public class Main {

    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();

        ObservableList<String> observableList = FXCollections.observableList(list);
        observableList.addListener(new ListChangeListener() {

            @Override
            public void onChanged(ListChangeListener.Change change) {
                System.out.println("Detected a change! ");
                while (change.next()) {
                    System.out.println("Was added? " + change.wasAdded());
                    System.out.println("Was removed? " + change.wasRemoved());
                    System.out.println("Was replaced? " + change.wasReplaced());
                    System.out.println("Was permutated? " + change.wasPermutated());
                }
            }
        });// => W W  w . y Ii B aI. CO  M
        observableList.add("item one");
        list.add("item two");
        System.out.println("Size: " + observableList.size());

    }
}

上面的程式碼生成以下結果。

Detected a change! 
Was added? true
Was removed? false
Was replaced? false
Was permutated? false
Size: 2