#Any recommended strategies of implementing an Action Listener that only listens when I call it.

1 messages · Page 1 of 1 (latest)

zinc ore
#

Hello, I have to code a rock, paper scissors GUI program with Java Swing using window builder on Eclipse.

I'm currently trying to figure out how to get user input and respond accordingly. I have 4 buttons; next round, rock, paper, and scissors. I only want either rock, paper scissors to do something only once for every round. However, right now I have an action listener on each of the buttons; rock, paper scissors, and it's responding to the action even though the user has not clicked next round each round.

This is the way I implemented for each button

btnRock.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //stuff
            }
        });

Anyone have any recommended way of handling user input on a conditional basis?

Thx in advance

serene pollenBOT
#

<@&987246527741304832> please have a look, thanks.

zinc ore
#

Also if anyone knows a GUI builder alternative on vs code pls lmk

left jetty
#

share state between the listeners

zinc ore
#

how do i do that

#

?

left jetty
#
class SharedState {
    boolean buttonClickedThisRound = false;    
}

...

SharedState state = new SharedState();
btnRock.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (state.buttonClickedThisRound) {
                    // stuff
                    state.buttonClickedThisRound = true;
                }
            }
        });

// make more action listeners
zinc ore
#

o ok thx

#

.solved

serene pollenBOT
# zinc ore .solved

Looks like you attempted to use a command? Please note that we only use slash-commands on this server 🙂

Try starting your message with a forward-slash / and Discord should open a popup showing you all available commands.
A command might then look like /foo 👍

zinc ore
#

/solved