【发布时间】:2016-12-15 10:49:29
【问题描述】:
我创建了一个动态组合框(在我的 TableView 的 CellFactory 类中初始化)来显示调用列表。它需要从 ID TableColumn 中读取 ID 号,然后从 DB 表中获取与该 ID 匹配的调用。一切顺利,但由于在我为表设置 CellFactory 时 TableView 没有完全初始化,我无法在运行时读取 ID,因此我将此代码移至 updateItem() 方法,设置调用列表单元工厂。
在 updateItem() (MyCellFactory) 中:
listCallCombo.setCellFactory(listview -> new ImageCallListCell(id_num));
listCallCombo.setButtonCell(new ImageCallListCell(id_num));
ImageCallListCell:
public class ImageCallListCell extends ListCell<String> {
private Label label = null;
private int id;
ImageCallListCell(int id) {
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
this.id = id;
}
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setItem(null);
setGraphic(null);
} else {
setText(item);
ImageView image = Utils.getImageViewByName("call");
if (image != null && !item.equals("")) {
image.setFitHeight(20);
image.setFitWidth(22);
String call = "Error";
if (item.equals("0")) {
call = "Add Call...";
} else if (id != 0 && !item.equals("-1")) {
call = ListCallHandler.getCallFromIDandIndex(id, Integer.parseInt(item));
}
label = new Label(call, image);
}
setGraphic(label);
}
}
}
ListCallHandler 返回一个调用数组,对数据库进行一次性提取。 这样做使我的代码正常工作,但当然每次为每一行设置 ButtonCell 和 CellFactory 都会使我的表格滚动缓慢。
我怎样才能更好地处理这个问题,以获得更好的性能?
提前致谢
编辑
这里是 ListCallHandler 代码 sn-p:
public static List<Map> getCalls() {
ResultSet list = DbUpdate.run_query("select * from memo", Utils.DBName);
List mCalls = new LinkedList();
try {
while (list.next()) {
Map call = new HashMap();
String callstring = list.getString("Lista Chiamate");
call.put("id", list.getInt("id"));
call.put("chiamate", callstring);
mCalls.add(call);
}
} catch (SQLException ex) {
...
}
public static String getCallFromIDandIndex(int id, int index) {
List<String> c = ListCallHandler.getCallsFromID(id, true);
String result = "(No Calls)";
if (index < c.size()) {
result = c.get(index);
}
return result;
}
getCallsFromID() 只需运行 getCalls,然后将数据重新组织到一个数组中。
再次感谢您!
编辑 2
getImageViewByName():
public static ImageView getImageViewByName(String name) {
return Utils.initImageView("raw/" + name + ".png");
}
public static ImageView initImageView(String path) {
BufferedImage bf = null;
WritableImage wr = null;
try {
bf = ImageIO.read(Utils.class.getResourceAsStream(path));
if (bf != null) {
wr = new WritableImage(bf.getWidth(), bf.getHeight());
PixelWriter pw = wr.getPixelWriter();
for (int x = 0; x < bf.getWidth(); x++) {
for (int y = 0; y < bf.getHeight(); y++) {
pw.setArgb(x, y, bf.getRGB(x, y));
}
}
}
} catch (IOException ex) {
System.err.println("ERROR LOADING PICTURE " + ex.getLocalizedMessage());
}
return new ImageView(wr);
}
我设法用一个小技巧让滚动变得不那么痛苦,但它的体验仍然很差,这就是我所做的:
tableview.addEventFilter(ScrollEvent.ANY, new EventHandler<ScrollEvent>() {
@Override
public void handle(ScrollEvent scrollEvent) {
if (scrollEvent.getEventType() == ScrollEvent.SCROLL_FINISHED) {
ListCallHandler.canUpdateCalls = true;
} else if (scrollEvent.getEventType() == ScrollEvent.SCROLL_STARTED) {
ListCallHandler.canUpdateCalls = false;
} else {
ListCallHandler.canUpdateCalls = true;
}
}
});
然后我在 ImageCallListCell 中绘制单元格之前检查了 canUpdateCalls 变量。慢慢滚动时,这很好。如果你跳到中间列表,它会挂断 4-5 秒...
【问题讨论】:
-
图片是不是一直都是一样的(从硬编码的参数到你的
Utils方法,好像是这样)。 -
是的,它是一个电话图标
标签: performance javafx combobox tableview