Note that there are some explanatory texts on larger screens.

plurals
  1. POPause and wait for button to be clicked, then continue
    primarykey
    data
    text
    <p>I am trying to build a simple tictactoe network game. I need the program to wait until a player makes a move and then continue. In my whileConnected() function at the bottom of my code, I have a while(true) cycle that is supposed to run forever and display a confirmation message when the button is pressed (which is signaled by the fact that the content of the String variable 'message' changes).</p> <p>The problem is that even if the String message variable changes when the button is clicked, my whileConnected() function never realizes this and the if statement inside the function never evaluates to true. An identical if statement inside the ButtonListener class works fine and displays the desired confirmation message.</p> <p>How can I solve this problem? I've read and read and I get the idea that I should use Threads (I read about them but I never used them before, that's why it's only a guess). Do I need threads? Can someone explain in short the principle that should be used for this specific problem? (how can I make the program pause until the button is clicked and then continue using relevant information created when the button was clicked). A code example would really lighten up my reading about threads - which is a really abstract topic when one is a beginner.</p> <p>Below is my code, thanks in advance.</p> <pre><code>public class Test extends JFrame { private Container contentPane; private JButton btn00; private static String message = ""; private class ButtonListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { String buttonText = e.getActionCommand(); if (buttonText.equals("My Button")) { message = "pressed"; if (message != "") System.out.println(message+"(executed by ButtonListener)"); } } } public Test() { this.contentPane = this.getContentPane(); btn00 = new JButton("My Button"); btn00.setSize(btn00.getPreferredSize()); btn00.setLocation(20,20); ButtonListener listener = new ButtonListener(); btn00.addActionListener(listener); // configure frame this.setSize(300,300); this.setDefaultCloseOperation(EXIT_ON_CLOSE); // make panel JPanel panel = new JPanel(); panel.setSize(200,200); panel.setLocation(10,10); panel.add(btn00); this.contentPane.add(panel); } public static void main(String[] args) { Test gui = new Test(); gui.setVisible(true); // connected whileConnected(); } private static void whileConnected() { System.out.println("message is at first empty: "+message); while (true) { // the if below never evaluates to true... why? if (message != "") // this is never true System.out.println(message+"(executed by whileConnected)"); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload