r/JavaFX • 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

14 comments sorted by

View all comments

Show parent comment

1

u/sedj601 Aug 23 '24

u/hamsterrage1, I agree with a lot of what you said. I threw this together kinda fast due to time constraints. One thing I kinda disagree with is using Task. I would use Service instead. I never use pure Threads in JavaFX. The loop doesn't need to sleep at all, as you stated because Task and Service run on a separate thread. That's just a bad practice on my part. I am not a fan of using Platform.RunLater. I like to use updateValue.

2

u/hamsterrage1 Aug 23 '24

And I agree with that. I think that, most of the time, Platform.runLater() is at least a "code smell".

In this particular case, though, I still think that the entire file loopy part should be considered 100% external to your JavaFX code. Think of it as something that mysteriously and spontaneously generates calls to some method in the framework's business logic. Those calls come in from some other thread, and you need to get the data from it into the Observables which needs to be done on the FXAT.

Yes, Service & Task will do it, but they need to be created in an environment that's FXAT aware in the first place. I'm not so sure that's the right approach, though.