Add EventHandler to ImageView contained in TilePane contained in VBox?
ImageView
s don't generate ActionEvent
s; so it is no surprise that your event handler is never invoked. Since the mouse event is not processed by the ImageView
, it propagates up to the container.
Try
img.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
System.out.println("Tile pressed ");
event.consume();
}
});
@James_D is correct; but note for Java 8 you can use the simpler syntax:
img.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
System.out.println("Tile pressed ");
event.consume();
});
Note that MouseEvent.MOUSE_CLICKED
is a class from javafx, not from java.awt.event
package
imgView. addEventHandler(javafx.scene.input.MouseEvent.MOUSE_CLICKED, event -> {
//event clicked here
});
For someone who mistakenly import java.awt.event.MouseEvent
class