1 year ago

#77002

test-img

rosalinaSpeedy

Is there a way to retain boolean value changed inside of an ActionListener once it's been changed inside the ActionListener?

i'm trying to make a poker game for a school project and im having particular difficulty changing this one variable. My code uses a while loop in the main of my class to handle data coming from the server, and as such data cannot be sent back to the server outside this loop. because of this, I need to change the value of the turnOver boolean variable within the ActionListener so that code in the while loop can be executed accordingly. However, when i try to change it, it's almost as if the value it's changed to is immediately disregarded once the ActionListener code is complete, and as such, the code within the loop never gets executed. The ActionListener is defined within the main.

Is there a way to 'retain' the value that the boolean turnOver is changed to so that i can use it again in the main? I've tried to explain this as best as i can and i will attach my code below (i'm pretty new to all this server/socket based stuff). Thank you for reading.

Relevant code for the actionListener:

public class Client {
public static Client bob = new Client();
private static boolean turnOver = false;
public static Table clientTable = new Table();


public static void main(String[] args) throws Exception {
    System.out.println("player joining");
    Scanner s = new Scanner(System.in);
    bob.startConnection("127.0.0.1", 8000);
    System.out.println("player joined");
    clientTable.betButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                System.out.println("value of turnOver: " + turnOver);
                Client.this.turnOver = true;
                System.out.println("value of turnOver: " + turnOver);
                System.out.println("bet button clicked");
            }

            catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    });

Relevant code for the while loop within the same main method:

    while (true) {
        System.out.println("value of turnOver before check " + turnOver);

        if (turnOver == true) {
            System.out.println("reached turn over status");
            [REST OF METHOD]
        }

With this current implementation, i never see the message "reached turn over status", and from that i assume that the value of turnOver is never true, even though i set it to be so in the ActionListener. How may i rectify this? Thank you.

java

sockets

while-loop

boolean

actionlistener

0 Answers

Your Answer

Accepted video resources