0

I'm currently building a Mobile app in JavaFx with the GluonMobile plugins and I try to find a way to know if the user just tap the button or if the button is pressed for over 2 second.

I try a couple of thing and I run into some problem.

The thing is I found this method on the web and he's my problem

First the code

button.addEventFilter(MouseEvent.ANY, new EventHandler<MouseEvent>() {

        long startTime;

        @Override
        public void handle(MouseEvent event) {
            if (event.getEventType().equals(MouseEvent.MOUSE_PRESSED)) {
                startTime = System.currentTimeMillis();
            } else if (event.getEventType().equals(MouseEvent.MOUSE_RELEASED)) {
                if (System.currentTimeMillis() - startTime > 2 * 1000) {
                    System.out.println("Pressed for at least 2 seconds (" + (System.currentTimeMillis() - startTime) + " milliseconds)");
                } else
                    System.out.println("Pressed for " + (System.currentTimeMillis() - startTime) + " milliseconds");
            }
        }
    });

The problem that I have here is really weird.

When I click the button the method work. If I click for over 2 sec it will printLn the good result.

BUT

If I click a second time on it it will print the PrintLn 2 times. If I click a 3rd time, it will print the result 3 time...

So the problem is when I put a method (let's say a dialogBox) the box will show 3 times.

It's like if the program had a memory of how many time I click it and give me the result the number of time I click the button.

Can I "reset" the memory of the method?

Thanks.

Jc

EDIT

   @FXML
   public void buttonClick(ActionEvent actionEvent) {

        btn1.addEventFilter(MouseEvent.ANY, new EventHandler<MouseEvent>() {

            long startTime;

            @Override
            public void handle(MouseEvent event) {
                if (event.getEventType().equals(MouseEvent.MOUSE_PRESSED)) {
                    startTime = System.currentTimeMillis();
                } else if (event.getEventType().equals(MouseEvent.MOUSE_RELEASED)) {
                    if (System.currentTimeMillis() - startTime > 2 * 1000) {
                        System.out.println("Pressed for at least 2 seconds (" + (System.currentTimeMillis() - startTime) + " milliseconds)");
                    } else
                        System.out.println("Pressed for " + (System.currentTimeMillis() - startTime) + " milliseconds");
                }
            }
        });


    }
Meilleur102
  • 25
  • 1
  • 8

0 Answers0