Prevent a second click on the same button

0

In this memory game, when I click the first button and then the second button, everything happens according to the algorithm, but when I click the first button twice, a problem happens ... I wanted to know how to avoid a second click on the first button clicked, type disable it so that it can not receive a second click ... because the button counts every click it receives, so there can not be more than one click.

            for (int i=0; i<16; ++i){

            if (event.getSource() == buttons[i]){
                buttons[i].setGraphic(new ImageView(imgs[Aleatorio[i]]));//novo código
                //buttons[i].setVisible(true);
                NumeroClick++;
                if (NumeroClick == 1) PrimeiroClick = i;
                if (NumeroClick == 2){
                    SegundoClick = i;

                    ///////////////Clicks_não_conseguidos///////////////
                    if (Aleatorio[PrimeiroClick] != Aleatorio[SegundoClick]){     

                        pontos-=2;

                    Servico service = new Servico();

                    service.setOnSucceeded((WorkerStateEvent ev) -> {

                        buttons[PrimeiroClick].setGraphic(null); //novo código
                        buttons[PrimeiroClick].setDisable(false);                            
                        buttons[SegundoClick].setGraphic(null); //novo código
                        buttons[SegundoClick].setDisable(false); 

                    });               

                        service.start();

                    }  else {
                        ContAcertos++;
                        pontos+=10;
                    }
                    NumeroClick = 0;
                }
            }
        }
    
asked by anonymous 05.06.2015 / 15:09

1 answer

2

A workable solution would be to create a Listener for when your button is clicked or undergo any changes, it changes its state until you want.

Another would be to add a Handler to the button.

Example 1:

Button x = new Button();
    x.onActionProperty().addListener(listenerEvent -> {
        x.setDisable(true);
    });

//...
// Até certo momento

x.setDisable(false);

Example 2:

Button x = new Button();
x.addEventHandler(ActionEvent.ACTION, event -> {
    x.setDisable(true);
});

//...
// Até certo momento

x.setDisable(false);

Take a look at this page in English that says exactly that!

    
09.06.2015 / 15:16