The generic solution is to use
event driven programming.
You shouldn't have sleeps or never ending loops on the JavaFX application thread, for reasons already covered in previous posts.
Specific to JavaFX,
event handlers are used.
Often, in JavaFX programming, the event handlers will set
properties.
Properties are slightly different from standard Java object getters and setters in that you can add listeners to the properties that will be triggered when the properties change.
So that gives you two options for responding to things, either directly by using event handlers attached to GUI controls and nodes OR indirectly by reacting to changes to properties that were updated by event handlers.
So, two simple ways to accomplish waiting for a player to take a turn are:
1. Just have a button on the screen which says next turn, set the
onAction event handler to trigger the logic you want to take effect when the user takes a turn. If you don't want a button but some other interaction to end a turn, (such as the player dragging a space ship to a sector), then another appropriate event can be used, for example
drag and drop event handlers.
2. You can set the logic of ending the turn based upon the state of the application. For example, a Game object might have multiple player objects and a property representing which players turn it is
. An event handler such as defined above in (1) can update the game state by notifying the game that the current players turn has completed, the game can work out who the next player is and set the currentPlayer to the next player due to take a turn. Any UI objects that need to update based upon the currentPlayer (for example highlight the name of the current player), can use a change listener on the currentPlayer property to see when it changes and update themselves accordingly.
Note, depending upon what you want to do, you might only need to code (1) and not (1 & 2).
=====
Note, the info below is a bit more advanced and not necessarily applicable to what you wish to do.
A model, view, controller approach can also be employed if you wish. The view and controller can be an FXML loaded document with its associated controller or some Java code which codes the graphics shapes and controls. The model can be a class which contains
properties which represent the application state and are set and updated as appropriate. For a large program, you can have shared model objects accessed through multiple views and controllers. Note that explicit definition of a model view controller system is not necessary for a very simple application as such an application can represent state directly in the controls (e.g. text in a text area or check state of a radio button), and react purely based upon event action handlers defined by the controls. It is only for larger apps or library components like controls that explicit models may become useful as they can be separated from the GUI and tested independently, concentrating domain logic inside the model and controllers and separated from the GUI code. See the
JavaFX UI controls architecture and
source code for a button for an example of this.