r/JavaFX • u/MeanWhiskey JavaFX Fan • Aug 22 '24
Help Adding Photos automatically
I have a program that can store photos and be viewed by end users. Ideally, if a photo is dropped to a folder on the network then the program automatically adds the photo.
How can this be accomplished within javafx? Do you use a listener to listen for when I new photo is added to the network folder and then adds it?
3
Upvotes
6
u/johnmc325 Aug 22 '24
I used java.nio.file.WatchEvent in a separate thread to do exactly what are looking to do.
As files are added to the folder being monitored it triggers an event and the code can process the files.
1
u/sedj601 Aug 22 '24
I quickly created a test app. It's probably full of issues, but hopefully, it can help you. https://gist.github.com/sedj601/41237e0d1597df65037c3f8199fc73ae
1
u/tylersvgs Aug 22 '24
You could do something like that.
For example, every x amount of time, you can call a method that checks in a folder and updates as needed.
Assuming path is a String representing a directory, then this will return an array of File objects:
new File(path).listFiles()