0

I'm doing some kind of social, it's when i open a post a new scene is created which initializes the elements with the data taken from the database. the problem is that the big photos take time to load, and until they load the program does not respond, I would like a way to be able to open the scene first so as to wait until the image loads without the program crashing

   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());
    }

here is the code, which is executed as soon as the scene loads. I was thinking of doing another DAO to first fetch all the data except the photo, load the scene and only then fetch the image or something like that, but I don't know how to do it

felix
  • 13
  • 1
  • 1
    Images can be [loaded in the background via a constructor parameter](https://openjfx.io/javadoc/19/javafx.graphics/javafx/scene/image/Image.html#%3Cinit%3E(java.lang.String,boolean)). But your performance issue is probably the networked database access, not the image loading. You can [use a task to run the database access concurrently](https://stackoverflow.com/questions/14878788/javafx-background-thread-for-sql-query). Perhaps this is a duplicate of that question. – jewelsea Jan 01 '23 at 13:45
  • "I was thinking of doing another DAO to first fetch all the data except the photo, load the scene and only then fetch the image or something like that, but I don't know how to do it" -> yes, you could do that as well. I don't know if you really need to or not. If you decide to do that, split the problem apart. Get the database access as you need it with two separate queries. If stuck on that, ask a new question with just the database portion in an [mcve]. Then once that is working integrate with the UI. – jewelsea Jan 01 '23 at 13:51
  • Have a look at this article here, it will tell you how to do what you need: https://www.pragmaticcoding.ca/javafx/elements/fxat – DaveB Jan 01 '23 at 15:46

1 Answers1

0

I think it is always a good idea to create and show a GUI in the empty state quickly first and then launch some background task to collect the data and once this data is available update the GUI. Just think of a word-processor GUI. When you launch it it's in the empty state without any document. Then the user selects a document which gets loaded and as soon as the data is available the GUI changes and displays the document. The only difference in your case is that you already know which data you want to load.

mipa
  • 10,369
  • 2
  • 16
  • 35