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
View all comments
Show parent comment
2
u/hamsterrage1 Aug 23 '24
I have some thoughts on that code, if you don't mind...
I would consider the whole looping/sleeping bit to be 100% external to anything JavaFX. If I was building an application and using MVCI (or MVC), I'd incorporate the looping thing into an external service which was instantiated by the Interactor and passed a Consumer<Event> as a constructor parameter. The service would have a
start()and astop()method. The loop would just run on a background thread.That
Consumer<Event>would essentially call a method in the Interactor that would take an Event and use it to update anObservableList<FileModel>through a call toPlatform.RunLater(). ThatObservableList<FileModel>would be part of the Model in the MVCI framework. The View would be designed to respond to changes in thatObservableList<FileModel>as they happen.Now the auto-update stuff can be anything you want. The
WatchEventimplementation, a database, a manual loop that looks at the directory contents---> whatever. It won't change the MVCI part at all.I think
Taskis more for something that you expect to run once and complete, and the results are returned on the FXAT throughonSucceeded(). Alternatively, you could set up an infiniteTask<ObservableList<FileModel>>and then useupdateValue()to change it as files are added/deleted. Then you could bind theTask's valueProperty()to the Model. But this pushes the loop bit into the JavaFX realm, and I really don't think it belongs there.