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