How to properly use JavaFX TableView and ObservableList classes?

我的梦境 提交于 2020-01-06 12:57:20

问题


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?


回答1:


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);
    }
}


来源:https://stackoverflow.com/questions/23730904/how-to-properly-use-javafx-tableview-and-observablelist-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!