Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

125
Visualizações
How can I use timeline for double click in JavaFX?

This is what I have done so far, it works just fine, but when I double click, the double click event is fired and the single click event is fired, when I single click only the single click is fired of course.

long currentMillis = System.currentTimeMillis();

addEventFilter(MouseEvent.MOUSE_CLICKED, e -> {

  long currentClickedMillis = System.currentTimeMillis();
  long diffMillis = currentClickedMillis - currentMillis;
  currentMillis = currentClickedMillis;
  if (diffMillis > 500) {
    fireEvent(new FlightListEvents(FlightListEvents.FLIGHT_BUTTON_CLICKED, this.aircraft));
  } else {
    fireEvent(new FlightListEvents(FlightListEvents.FLIGHT_BUTTON_DOUBLE_CLICKED, this.aircraft));                   
  }
});

The other solutions on Stack Overflow do not work! They just count numbers of clicks and still have the same problem I have now.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I had the same problem once and couldn't find the solution too, so I had to develop my own solution.
For that I used Javafx's Timeline which is gonna give you the ability to start a time line at your first click and depending on if you afterward make a second click or not, it is gonna consider it to be either a single click or a double click, but never both at the same time.
Here is my solution:

// initialize your timeline
final Timeline clickTimeline = new Timeline();
// minimum duration of a double click 500ms
Duration clickDuration = Duration.millis(500);
KeyFrame clickKeyFrame = new KeyFrame(clickDuration);
boolean isClickTimelinePlaying = false;
clickTimeline.getKeyFrames().add(clickKeyFrame);

addEventFilter(MouseEvent.MOUSE_CLICKED, e -> {
  if (isClickTimelinePlaying) {
    // when clicking the second time before the time line finishes
    isClickTimelinePlaying = false;
    fireEvent(new FlightListEvents(FlightListEvents.FLIGHT_BUTTON_DOUBLE_CLICKED, this.aircraft));
    clickTimeline.stop();
  } else {
    // when clicking for the first time
    isClickTimelinePlaying = true;
    // start the timeline
    clickTimeline.play();
  });
}

// if timeline finises without receiving a second click, consider it a single click
clickTimeline.setOnFinished(event -> {
  fireEvent(new FlightListEvents(FlightListEvents.FLIGHT_BUTTON_CLICKED, this.aircraft));
  isClickTimelinePlaying = false;
});

               
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda