The count number of pressed button


Question

I need that when I press the button to show me how many times was the button pressed. I use this method, but on console still show me the number 1.

Here is code:

 button_help.setOnMousePressed(new EventHandler<MouseEvent>() {

    @Override
    public void handle(MouseEvent event) {

    int count = 0;
    count ++;

        System.out.println(count);
    }


 });
1
1
10/23/2014 8:58:42 AM

Your solution doesn´t work as you are reseting the value of variable every time you click button. You have to define it once and than just increase the valu of it.

Solution:

int count = 0;

button_help.setOnMousePressed(new EventHandler<MouseEvent>() {

    @Override
    public void handle(MouseEvent event) {
        count++;
        System.out.println(count);
    }

});
3
10/23/2014 12:47:11 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon