Manuel
Manuel

Reputation: 802

Waiting for a click input after a button press Java Fx

Im having some issues trying to wait for an input after clicking a button.

With my team, we are making a card game, in which cards attack one another, the problem is that i don't know how to, after a button is clicked, make the event handler wait for the user to click another button.

The code looks like this:

private Button attackingButton(){
    Button b1 = new Button();
    b1.setOnAction(new EventHandler<ActionEvent>()
    {
        public void handle(ActionEvent event){
            //Here i want the user to press another button and, depending which one he 
            //pressed, asing a variable
            Card aCard = //The card that the button pressed has inside
        }
    }

Upvotes: 0

Views: 874

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

That's just it, you don't make the handler wait. Instead you change the behavior of the handler depending on the state of the object. If the object is in the "user has not pressed the first button yet" state, the handler does one thing. If the object is in the "user has previously pressed the first button", then the handler does something else. Your handler should query the state of the object's instance fields to determine this state.

e.g.,

b1.setOnAction(new EventHandler<ActionEvent>() {
    public void handle(ActionEvent event){
        // may need to use boolean fields or .equals(...) method......
        if (someStateField == someValue) {
            doBehavior1();
        } else {
            doBehavior2();
        }
    }
}

Upvotes: 1

Related Questions