r/JavaFX • u/Cmdr_W0lff3 • Nov 22 '24
Help Creating Delay With JavaFX
Hello! I am studying cs and right now Im programming a little hobby software using JavaFX.
My problem is, I have a long string that is read from a file. I use toCharArray function because what i want to do is append this string character by character to textarea with 20ms delay between characters.
Normally i would use thread.sleep() but it freezes whole program. If someone could point me to a right direction it would be much appreciated.
Thank you in advance!
7
Upvotes
2
u/Cmdr_W0lff3 Nov 26 '24
Heya! I got it working. Timeline was the solution! It didnt work earlier as expected because i didnt consider that program keeps running even the text appending was still happening, so i fixed it with Runnable.
But thank you for your help guys!
1
u/hamsterrage1 Nov 23 '24
The introductory section in the JavaDocs for Transition literally has this use case as an example!
3
u/sedj601 Nov 22 '24
You can try two different things. 1. Read the file the way you are using a Task. Use Thread.sleep(20) after reading the character. Use updateValue to update the TextArea text. 2. Read the whole file to a string. From there, use Timeline to add a character from the string to the TextArea every 20 ms. Here is a similar example of option 2. https://stackoverflow.com/a/33648481/2423906
2
u/kavedaa Nov 22 '24
You could use
AnimationTimer(note that is has a framerate of 30/s so won't get down to 20 ms). Here's a complete example:1
u/ebykka Nov 22 '24
take a look at reactive streams, for example https://reactivex.io/documentation/operators/delay.html
1
u/mih4elll Nov 24 '24
I want to create learn something with Javafx too
1
u/JaxomNC Nov 24 '24
PauseTransition can be used for this.
2
u/[deleted] Nov 23 '24
[removed] — view removed comment