I searched around for this and couldn't get a clear answer. I've written a game that needs to pause until the user clicks a button with their decision, and then continue to execute. Is there a standard way to do this?
I've seen similar questions that refer to using 'wait()' and 'notify()', but I wasn't sure I needed to add more threads, especially since I'm not executing complex or time-consuming code.
I should clarify it's a computer version of a board game, so nothing more than a frame with some components. Here's some of what I'm trying to do, thanks guys:
public class TreasureHunterFrame extends javax.swing.JFrame
{
public TreasureHunterFrame()
{
initComponents();
startNewGame();
}
private void startNewGame()
{
...
// User asked to click button while this method is running
synchronized (this) // wait until Stay of Leave button is clicked
{
try
{
while (!userHasMadeDecision)
this.wait();
}
catch (InterruptedException ie)
{
}
}
....
}
private void userStayButtonActionPerformed(java.awt.event.ActionEvent evt)
{
userHasMadeDecision = true;
userLeaving = false;
synchronized (this)
{
notifyAll();
}
}
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TreasureHunterFrame().setVisible(true);
}
});
}
}