Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

ImageView with transparent image not clickable

user8929955Dec 29 2014 — edited Jan 3 2015

I created an ImageView with an image with transparent portions. I also did some scaling and moving, which I though might cause this effect, but that is not the case. When I click on the transparent portion of the image the onMouseClick event is not fired. Other mouse events (mouse enter, mouse move, mouse exit, ...) get fired. Only when I click on the non transparent part the click event is fired.

Image img = new Image(getClass().getResourceAsStream("barrel_icon.png"));

ImageView view = new ImageView(img);

view.setScaleX(0.5);

view.setScaleY(0.5);

view.setOnMouseClicked(new EventHandler<MouseEvent>() {

@Override

public void handle(MouseEvent mouseEvent) {

System.out.println("Clicked on the scaled icon");

}

});

StackPane root = new StackPane();

Group group = new Group(view);

group.setManaged(false);

group.setLayoutY(-13);

group.setLayoutX(-13);

root.getChildren().add(group);

TransparentClickableImage.png

The area between the barrel and the shadow belongs clearly to the image, however is transparent.

I am wondering if there is a reason for this behaviour and what is the best way to work around this issue, as my image has large transparent portions and the non transparent parts are hard to click on.

By the way this is all based on JavaFX 2.2.

Regards

Hotzst

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 31 2015
Added on Dec 29 2014
2 comments
3,284 views