tags:

views:

695

answers:

4

Is there a way to force a JavaFX app to repaint itself before proceeding? Similar to a Swing Panel's paint(Graphic g) method (I might be getting the keywords wrong there).

Consider the following example: you write a TicTacToe app along with the AI required for a computer player. You would like the ability to show two computer players duke it out. Maybe you put in a two second pause between computer turns to give it a life-like affect. When you hit your "Go" button, there's a large pause of unresponsiveness (the time it takes for the 9 turns to go by with faked pauses for the computer to 'decide') and then suddenly the app's visual is updated in with the completed game's state.

It seems like JavaFX repaints once processing in the app's thread is finished? I'm not completely sure here.

Thanks!

A: 

First: In Java, you wouldn't call paint(Graphics g) directly. You'd call repaint(), which requests that paint() be called as soon as possible.

Second: I don't think that claim is accurate, but maybe I'm misunderstanding it. If a JavaFX app didn't repaint until the app is finished, there wouldn't be a need to paint anything, since the app is done. Could you post a sample problem, or illustrate further?

David
+2  A: 

You are right. JavaFX is event-driven and single-threaded. This means that repaint and event response can not be done simultaneously. Long-running task should be executed on separate thread so they do not block the rendering of the UI, When the task is finished it can sync back to the FX thread by calling FX.deferAction() which will simply execute the code on the main thread.

Honza
+1  A: 

This won't be the most helpful answer as I have toyed around with JavaFX for all of half a day, but wouldn't you use Timelines, Keyframes, and binding to accomplish your repaints instead of calling them explicitly like you have described?

See this tutorial for an example.

whaley
A: 

JavaFX's model is to separate you from the painting of the "stuff" on the screen. This is very powerful but is a change from how you might be familiar with.

whaley is correct that the appropriate way of doing this in JavaFX is to make a timeline where the move is done every X seconds and will be drawn at that keyframe.

If you have a question about how to do this, try it and make a new question with some code.

MattGrommes