如何正确使用JavaFX TableView和ObservableList类? [英] 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;
}
}
后来,在另一个班级,我将的内容关联到TableView
.但是,我创建了一个FXCollections.observableArrayList()
,它被设置为表的绑定元素.
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.
这是我的方法:
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);
tableResultados是TableView
,而声明为ObservableList<Pelicula>
的鹈鹕自然不会初始化.
Where tableResultados is a TableView
and peliculas declared as an ObservableList<Pelicula>
naturally not initialized.
我讨厌的是,我认为那不是很好,当我需要在YIFY
类上更改/更新resultados
时,我还需要这样做:
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());
自从YIFY
课开始以来,我认为所有这些都应该是 Observable
,但我遇到了很多问题,但未找到沿着Java文档,适当的类实现不是抽象的,因此我不必实现任何其他方法.
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?
推荐答案
我不确定我是否真的理解了这个问题,但是那是怎么回事
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);
}
}
这篇关于如何正确使用JavaFX TableView和ObservableList类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!