diegoaguilar
diegoaguilar

Reputation: 8376

How to properly use JavaFX TableView and ObservableList classes?

I got a class where I receive some collection structure:

public class YIFY {

    private static List<Pelicula> resultados;

    public static void setResultados(List<Pelicula> resultados) {
        YIFY.resultados = resultados;
    }

}

Later, at another class I associate the contents of such List to a TableView. However I create an FXCollections.observableArrayList() which is set as the bind element to the table.

This is how I do it:

    peliculas = FXCollections.observableArrayList(YIFY.getResultados());

    tituloColumn.setCellValueFactory(new PropertyValueFactory<>("titulo"));
    calidadColumn.setCellValueFactory(new PropertyValueFactory<>("calidad"));
    imdbColumn.setCellValueFactory(new PropertyValueFactory<>("imdbLink"));
    añoColumn.setCellValueFactory(new PropertyValueFactory<>("año"));
    tableResultados.setItems(peliculas);

Where tableResultados is a TableView and peliculas declared as an ObservableList<Pelicula> naturally not initialized.

What I hate and I think is just not that OK is that when I need to change/update resultados at YIFY class, I also need to do:

peliculas.clear();
peliculas.setAll(YIFY.getResultados());

I think it all should be an Observable since the beginning at YIFY class, what I tried but I got MUCH problems as I didn't find along the Java docs a proper class implementation which was not abstract so I wouldn't have to implement any extra method.

How can I manage this? Is my approach ok?

Upvotes: 0

Views: 395

Answers (1)

James_D
James_D

Reputation: 209225

I'm not sure I really understand the question, but what's wrong with

public class YIFY {
    private static ObservableList<Pelicula> resultados = FXCollections.observableArrayList();

    public static void setResultados(List<Pelicula> resultados) {
        YIFY.resultados.setAll(resultados);
    }
}

Upvotes: 1

Related Questions