What are the ObservableList and where can I use them? [closed]

2

I start programming in java and I have not been clear about the observable lists, I have seen examples where they relate them to an ArrayList but I do not understand what the end of that relationship is.

    
asked by JEs 19.04.2017 в 03:15
source

1 answer

3

The ObservableList interface is a list interface of JavaFX , and use FXCollections to equip a list with additional functionality.

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

public class EjemploObservableList {

    public static void main(String[] args) {

        // Se insta una lista.
        List<String> list = new ArrayList<String>();

        // se agrega la observación usando FXCollections:
    ObservableList<String> observableList = FXCollections.observableList(list);
        observableList.addListener(new ListChangeListener() {

            @Override
            public void onChanged(ListChangeListener.Change change) {
                System.out.println("Ocurrio un cambio! ");
            }
        });

        // Se reportan cambios a observableList.
        // Aquí se va imprimir la alerta 
        observableList.add("uno");

        // cambios directas a la lista escapan la observación
        // no se imprime ninguna alerta 
        list.add("dos");

        System.out.println("Tamano: "+observableList.size());
    }
}
    
answered by 19.04.2017 в 06:46