Double click is not being caught by OnMouseEvent (

2019-08-01 18:25发布

问题:

Double clicks are being captured as single click :(

In fxml file:

<Button fx:id="A_button" onMouseClicked="#buttonAClicked">

In the controller

private void buttonAClicked(MouseEvent mouseEvent) {
    if (mouseEvent.getButton().equals(MouseButton.PRIMARY)) {
        if (mouseEvent.getClickCount() == 2) {
            System.out.println("Double clicked A_button");
        }
        if (mouseEvent.getClickCount() == 1) {
            System.out.println("Single clicked A_button");
        }
    }
}

Unfortunately I am finding that double click is not being caught - only single clicks. In the debugger, click count is 1.

Update: Since I cant figure out why it is not working for me on JavaFX 2.2.3-b05, I did a workaround and removed the need for double-click. I added a "Load" button to the UI. Now user must single click AND press the load button.

回答1:

It was fixed in JavaFX 2.2, see http://javafx-jira.kenai.com/browse/RT-19346

Note, that on double click you will receive two events:

  • mouse click with getClickCount() == 1
  • mouse click with getClickCount() == 2

E.g. if you run code below and double click on button output would be:

clicks: 1
clicks: 2

Code (tested with 2.2.4):

public class DoubleClicks extends Application {
    @Override public void start(Stage stage) {
        Button btn = new Button();
        btn.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                System.out.println("clicks: " + event.getClickCount());
            }
        });

        stage.setScene(new Scene(new Group(btn), 300, 250));
        stage.setTitle(VersionInfo.getRuntimeVersion());
        stage.show();
    }

    public static void main(String[] args) { launch(); }
}


标签: javafx-2