I have made a program which loads approximately 66 images into the program when it is run. I load the images using a SwingWorker so that I can access the images quickly without having to load them when I click on a button. However, the loading takes a bit long (2+ minutes). I want to pre load the images so that when the user runs the program, the images are already loaded. I have searched it on the internet but haven't found a workaround. Is there a way to do this? Any help would be highly appreciated. Thankyou.
Asked
Active
Viewed 169 times
-2
-
@AndyTurner I suggest you make an Answer of your comment, so it can be accepted to close this Question. – Basil Bourque Sep 16 '18 at 16:56
-
@BasilBourque done. – Andy Turner Sep 16 '18 at 17:58
2 Answers
2
You can only load them at runtime, so if they take 2 minutes to load, then they will always take 2 minutes to load.
It's just a question of when that two minutes starts from (either when the button is pressed, or from some earlier point where it makes sense to start loading them).
If the images are taking this long to load, it might be worth considering why they are taking so long. For example:
- You may be able to reduce the loading time by loading smaller images, at least in the first instance.
- If you are downloading the images from a remote source, store them locally instead (I guess this is almost too obvious to state, though).

Andy Turner
- 137,514
- 11
- 162
- 243
-
The images load when the program runs, a Progress bar is used to show the loading status. They take time to load as they are large images. I can't load smaller ones as I need to zoom in on these large images afterwards. For your second suggestion, they are already stored locally – Muhammad Umer Sep 16 '18 at 18:13
-
2@MuhammadUmer "as I need to zoom in on these large images afterwards" do you need to zoom in on 66 images simultaneously? I'd be surprised if you did; load all images in low res, then load the high res image only when you want to zoom in on that specific image. – Andy Turner Sep 16 '18 at 18:15
-
The whole point of loading the high resolution images at first is because if I load a high res image on a button click, the program lags for a second and then displays the image. I need to load the high res images before clicking on the buttons instead of loading low res first and loading high res after. I hope you understand – Muhammad Umer Sep 16 '18 at 19:03
-
-
it's not about what's worse my friend, it's about what the client wants – Muhammad Umer Sep 16 '18 at 19:10
-
@MuhammadUmer well, I bet you a donut that the client doesn't want a 2 minute pause. If you don't want to consider doing it another way, you've got to sell the client that pause. – Andy Turner Sep 16 '18 at 19:22
0
Put them in your classpath and load them at runtime as resources. You will need to know all 66 names.
See the official Oracle tutorial at https://docs.oracle.com/javase/8/docs/technotes/guides/lang/resources.html to learn more.

Thorbjørn Ravn Andersen
- 73,784
- 33
- 194
- 347
-
how do I do that. Currently, I am accessing my files from a folder inside the project folder. Will moving my images to a 'Resources' folder speed up the process? – Muhammad Umer Sep 16 '18 at 17:25
-
You need to read up on how resources work first. https://docs.oracle.com/javase/8/docs/technotes/guides/lang/resources.html – Thorbjørn Ravn Andersen Sep 17 '18 at 22:56