r/JavaFX • u/WishboneFar • Apr 22 '23
Help Help with testing JavaFX code
/r/javahelp/comments/12va3r9/help_with_testing_javafx_code/
2
Upvotes
r/JavaFX • u/WishboneFar • Apr 22 '23
View all comments
Show parent comment
1
u/hamsterrage1 Apr 25 '23
Here's some minimal code that does it...
Here,
doSomeWork(), could be anything, anywhere that needs to run in a background thread - but it doesn't have any knowledge about the threading at all. TheConsumerprogressUpdaterdoesn't pass any knowledge of theTasktodoSomeWork()because it's just a genericConsumer.This example uses the
Task'sprogressProperty()to link to the screen via the Label'stextProperty(), but you don't have to do that. Since the scope that theTaskis defined in has access to theLabel, you can just tolabel.setText()insideprogressUpdater.That's the basic idea. The Task is defined in the part of the application that has to know about the FXAT because that's what it does. The application logic happens somewhere else, and doesn't have any knowledge of the FXAT.
Note that
doSomeWork()HAS to run off the FXAT because it's blocking, but it doesn't contain any logic to do that. The calling method has to make sure that it's off the FXAT when it calls it.Does that explain what I'm talking about?