Pilling two obj with OnMousePressed on top of each other in javafx

时间:2015-07-31 20:56:31

标签: java javafx

Been trying around and searching but couldn't find any solution, so I finally decided to give up and ask further...

Creating a javafx app, I load tiles in a TilePane.

This tiles are clickable and lead to a details page of their respective content.

On each tile, if they do belong to a certain pack, I do display the pack name, that is also clickable and lead to a page showing that specific pack content.

So that means the container, the tile, that is a Pane is clickable and on top of it I have a Label that is claickable also. What happens is when I do click the Label, it also triggers the Pane onMousePressed()... Here is a part of the tile creation code, the part focused on the onMousePressed(). I tried to make the Pane react by double click and the Label by single, it works, but I want to Pane to open with a single click.

I would be more than thankfull for any ideas how to solve that.

public DownloadTile (Downloadable upload, MainApp mainApp) {
    _mainApp = mainApp;
    _upload = upload;
    _tile = new Pane();
    _tile.setPrefHeight(100);
    _tile.setPrefWidth(296);
    _tile.setStyle("-fx-background-color: #ffffff;");
    _tile.setCursor(Cursor.HAND);
}

public void refresh() {
    _tile.getChildren().clear();
    _tile.setOnMousePressed(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
            if (event.isPrimaryButtonDown() /*&& event.getClickCount() == 2*/) {
                _mainApp.showDownloadDialog(dt, _upload);
            }
        }
    });

    if (_upload.getPack() != null) {
        Label pack = new Label();
        pack.setText(_upload.getPack());
        pack.getStyleClass().add("pack-link");
        pack.setCursor(Cursor.HAND);
        pack.relocate(10, 48);

        _tile.getChildren().add(pack);

        pack.setOnMousePressed(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                if (event.isPrimaryButtonDown()) {
                    _mainApp.showPackPage(_upload);
                }
            }
        });
    }
}

1 个答案:

答案 0 :(得分:1)

您的标签将首先接收鼠标点击(因为它位于顶部),因此在您处理完点击后,您可以使用“消费”阻止它向下传递:

pane.setOnMouseClicked(
        (Event event) -> {    

    // process your click here

    System.out.println("Panel clicked");
    pane.requestFocus();
    event.consume();
};