我正在使用JavaFX作爲GUI的聊天應用程序。我在ListView中顯示聊天內容,但是我遇到了一個很大的問題 - 它非常慢。當我將新項目添加到列表中時,特別是當我向上/向下滾動列表時。我認爲也許它與這樣一個事實有關,即每次添加新項目(列表中的每個單元格)時,列表都會刷新它,並且每次向上/向下滾動時都會刷新。 有人知道我能做些什麼來解決這個問題嗎? TNXJavaFX ListView非常慢
我重寫的ListCell的的updateItem:
chatListView.setCellFactory(新的回調,的ListCell>(){ @覆蓋 公衆的ListCell調用(ListView控件P){ 的ListCell電池=新的ListCell(){ @Override 保護無效的updateItem(的UserInfo項,布爾億){ super.updateItem(項目,億);
if (item != null) {
BorderPane borderPane = new BorderPane();
ImageView profileImage = new ImageView(new Image(item.getImageURL()));
profileImage.setFitHeight(32);
profileImage.setFitWidth(32);
Rectangle clip = new Rectangle(
profileImage.getFitWidth(), profileImage.getFitHeight()
);
clip.setArcWidth(30);
clip.setArcHeight(30);
profileImage.setClip(clip);
SnapshotParameters parameters = new SnapshotParameters();
parameters.setFill(Color.TRANSPARENT);
WritableImage image = profileImage.snapshot(parameters, null);
profileImage.setClip(null);
profileImage.setImage(image);
ImageView arrowImage = new ImageView(new Image("arrow1.png"));
ImageView arrowImage2 = new ImageView(new Image("arrow1.png"));
Label nameLabel = new Label(item.getUserName());
nameLabel.setStyle(" -fx-text-alignment: center; -fx-padding: 2;");
HBox hbox = null;
Label textLabel = new Label();
String messageText = splitTolines(item.getMessage());
textLabel.setText(messageText);
textLabel.setStyle("-fx-background-color: #a1f2cd; "
+ "-fx-padding: 10;\n"
+ "-fx-spacing: 5;");
hbox = new HBox(arrowImage, textLabel);
VBox vbox = new VBox(profileImage, nameLabel);
BorderPane.setMargin(vbox, new Insets(0, 10, 10, 10));
BorderPane.setMargin(hbox, new Insets(10, 0, 0, 0));
//Time
Date dNow = new Date();
SimpleDateFormat ft = new SimpleDateFormat("hh:mm a");
Label timeLabel = new Label(ft.format(dNow));
timeLabel.setStyle("-fx-font: 8px Tahoma; -fx-width: 100%");
HBox hbox2 = new HBox(arrowImage2, timeLabel);
arrowImage2.setVisible(false);
VBox vbox2 = new VBox(hbox, hbox2);
borderPane.setCenter(vbox2);
borderPane.setLeft(vbox);
setGraphic(borderPane);
}
}
};
return cell;
}
});
你應該包括[MCVE](http://stackoverflow.com/help/mcve),否則你的問題是有效地無法回答。 – jewelsea 2014-11-24 22:45:03
是的,請提供示例代碼。你重寫ListCell的'updateItem'方法嗎?你在那裏做什麼昂貴的東西? [Flowless](https://github.com/TomasMikula/Flowless)可能會或可能不會幫助你的情況。 – 2014-11-25 01:01:18
我添加了一些代碼^ – Yael 2014-11-26 14:01:49