• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Timer exception when adding shapes

 
Casey Clayton
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I am having an issue with a exception when trying to add new shapes to my JavaFx program. I have a randomizer that will randomly create a new Box object, which is converted to a rectangle for JavaFx use by a method I wrote, which all works fine. But when I setup the timer everything will run perfectly fine the first time through but then fails when it gets to this line when the timer kicks in. This is what the entire method looks like.



Here is using a timer to call the generateShapes method every 2 seconds or so.(As you notice I have a while loop that is commented out, which works perfectly fine but I need a timer which is causing the Thread exception.


If you need more information then let me know.
 
John Damien Smith
Ranch Hand
Posts: 259
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wrap the contents of your run method in Platform.runLater to put it on the JavaFX Application thread. Or remove the Timer and replace it with a Timeline.
 
Casey Clayton
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Damien Smith wrote:Wrap the contents of your run method in Platform.runLater to put it on the JavaFX Application thread. Or remove the Timer and replace it with a Timeline.


Ok so I switched the method over to this but it's saying "void" type not allowed here, what am I doing wrong?

 
John Damien Smith
Ranch Hand
Posts: 259
13
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to stick with a timer, see this StackOverflow answer on timers and JavaFX:


Though, I'd still be inclined to prefer a Timeline based approach as outlined in Tomas's answer to the same question:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic