【发布时间】:2023-01-02 02:49:05
【问题描述】:
我正在做某种社交活动,当我打开一个帖子时,会创建一个新场景,该场景使用从数据库中获取的数据初始化元素。问题是大照片需要时间来加载,并且在加载程序之前没有响应,我想要一种能够先打开场景的方法,以便等到图像加载而不会导致程序崩溃
public void init(int idpost) throws SQLException {
this.post = new PostDAOImpl().getPost(idpost);
photo.fitWidthProperty().bind(imgContainer.widthProperty());
photo.fitHeightProperty().bind(imgContainer.heightProperty());
photo.setImage(new Image(post.getPhoto()));
name.setText(post.getProfile().getName());
username.setText("@" + post.getProfile().getUsername());
if (post.getProfile().getAvatar() != null)
avatar.setImage(new Image(post.getProfile().getAvatar()));
description.setText(post.getDescription());
}
这是代码,场景加载后立即执行。我正在考虑做另一个 DAO 来首先获取除照片之外的所有数据,加载场景,然后才获取图像或类似的东西,但我不知道该怎么做
【问题讨论】:
-
图片可以是loaded in the background via a constructor parameter。但是您的性能问题可能是网络数据库访问,而不是图像加载。你可以use a task to run the database access concurrently。也许这是该问题的重复。
-
“我正在考虑做另一个 DAO,首先获取除照片之外的所有数据,加载场景,然后才获取图像或类似的东西,但我不知道该怎么做” -> 是的,你可以做那也是。我不知道你是否真的需要。如果您决定这样做,请将问题分开。通过两个单独的查询获取您需要的数据库访问权限。如果坚持下去,请在 minimal reproducible example 中提出一个仅包含数据库部分的新问题。然后一旦可以与 UI 集成。
-
在这里看看这篇文章,它会告诉你如何做你需要的:pragmaticcoding.ca/javafx/elements/fxat
标签: multithreading performance javafx imageview