I have recently started exploring Java FX and want to create a custom label which will have an ImageView inside it.
This is the code for my custom label.
Image image = new Image(getClass().getResourceAsStream("/img/remove.png"), 20, 20, true, true);
ImageView removeImageView = new ImageView(image);
Label customLabel = new Label(labelText, removeImageView);
customLabel.setFont(Font.font("Arial", FontWeight.BOLD, 20));
This is how my custom label looks.
Now I want to add a mouse click EventHandler to the ImageView. This is my code for handling the mouse clicks.
removeImageView.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
System.out.println("Imageview Clicked");
}
});
But when I am clicking on the cross image, the event is not getting captured.
I experimented a bit and tried to add an EventHandler to the customLabel. The label was able to capture the mouse click.
It seems to me that I am facing this issue because the ImageView is contained within the Label. What I want to know is, is this a limitation to JFX or is there any alternative way to achieve this functionality. Thanks.

ImageView removeImageView = new ImageView(image);- Rito