JavaFX TableView性能问题 [英] JavaFX TableView performance issue
问题描述
我正在使用javafx表视图控件来显示我的表数据。现在数据非常庞大,当我在控件中显示完整数据时,它会崩溃。有没有办法这样做或者我必须缓存数据,以便我可以以块的形式显示它。我应该使用JTable吗?
I am using javafx table view control to display my table data. Now the data is quite huge and when I show the full data in the control it crashes. Is there any way to do so or will I have to cache the data so that I can display it in chunks. Should I use JTable instead ?
推荐答案
我使用CellFactory类对tableview进行了延迟加载...我会尝试解释一下,任何英语都比我好的人都可以自由编辑并使其更容易理解!
I made a lazy-load on tableview using the CellFactory class... I´ll try to explain, anyone with better english than mine fell free to edit and make it more understandable!
首先我在Bean中使用强制属性来检查该bean是否已经存在已加载或未加载。这样我就不需要专门为它创建一个属性。
First I use a mandatory property in a Bean to check if that bean has already been loaded or not. This way I don't need to create an attribute specifically for that.
其次,我加载到TableView我的Bean集合只是加载了'id'属性,是什么让我的表格全部显示为空白。
Second, I loaded into TableView my Bean collection just with the 'id' property loaded, what makes my table display all in blank.
最后,我创建一个CellFactory,如下所示,检查对象是否已加载,如果现在不加载。
At last, I create a CellFactory, like this one below, to check if object has loaded or not, and if not do it now.
namecolumn.setCellFactory(new Callback<TableColumn<PersonBean, String>, TableCell<PersonBean, String>>() {
@Override
public TableCell<PersonBean, String> call(TableColumn<PersonBean, String> param) {
TableCell<PersonBean, String> newtablecell = new TableCell<PersonBean, String>() {
@Override
protected void updateItem(String item, boolean empty) {
// Since I got some strange errors when method getIndex() return greatter value than my collection size, I need to validate before getting the object
if (getIndex() < getItems().size()) {
//Retrieve the collection object
PersonBean personbean = getItems().get(getIndex());
// Name is mandatory, so if it is null my bean was not loaded yet
if (personbean.getName() == null) {
// Call other centralized method to make load, details explained later
loadPersonBean(personbean);
}
// Now I put the value on TableCell. Remember "item" still empty so we need to use Bean
setText(personbean.getName());
setAlignment(Pos.CENTER);
}
}
};
return newtablecell;
}
});
在方法loadPersonBean中,我传递TableView Collection中包含的对象并从DataBase加载它。比我将所有需要的数据复制到从表中收到的bean。我没有尝试在TableView中交换对象,但我相信它可能会导致并发异常。
In the method loadPersonBean I pass the object contained in TableView Collection and load it from DataBase. Than I COPY all the needed data to the bean received from the table. I didn't try to swap the object in TableView, but I believe it may result a Concurrent Exception.
其他观察:在我的所有测试中,TableView始终遵循列顺序调用我的CellFactory,但是如果用户交换列顺序,我没有测试它是否仍然遵守订单。所以我更喜欢在所有列中添加一个带有LazyLoadCheck代码部分的CellFactory。
Other observation: In all my tests TableView always respects the column order to call my CellFactory, but I did not test if it still respects the order if user swap the column order. So I prefer to add a CellFactory with the "LazyLoadCheck" part of code to all columns.
希望我能够清楚地帮助我!如果仍有问题,请发表评论,我会尝试做出更好的解释。
Hope I made my self clear enough to help! If any questions remains, comment and I'll try to do a better explanation.
这篇关于JavaFX TableView性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!