Add EventHandler to ImageView contained in TilePane contained in VBox?


I have the following schema:

  • A VBox, containing a HBox and a TilePane.

In HBox are buttons, labels, and text fields.

Every time I click on the root (HBox), I should add a ImageView to the tile pane. This ImageView shold contain an image (example: "2.jpg"). Maximum of tile pane components is 5.

Every time I click the image, i should load a new image to the clicked ImageView, exemple "1.jpg". It is not working. When I click on my image it is like i'm clicking on the root so it creates another cell of TilePane. Here's the code, can you help me?

 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.

package dadao1;

import java.util.HashSet;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.TilePane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

 * @author Ambra
public class Dadao1 extends Application {
    VBox root;
    HashSet dadi = new HashSet();
    //static int numeroDadi = 0;
    public void start(Stage primaryStage) {       

        Scene scene = new Scene(root, 300, 300);

        primaryStage.setTitle("Hello World!");
     *This private method sets up the GUI.
     * No parameters are required
    private void setGui(){
        root = new VBox();
        HBox buttons = new HBox();
        final Button newGame = new Button("New Game");
        final Button print = new Button("Print");
        final Button animation = new Button("Moving");
        animation.addEventHandler(ActionEvent.ACTION, new EventHandler<ActionEvent>() {
            // this button is labeled "Moving" at the begin. If pressed it changes its label to "Dissolving" and viceversa.
            public void handle(ActionEvent event) {
                if (animation.getText().equals(new String("Moving")))
                else animation.setText("Mooving");
        final Label score = new Label("Total");
        final TextField points = new TextField();
        final Label pointsLabel = new Label("Score");
        root.setStyle("-fx-background-color: green");

        final TilePane dadiPane = new TilePane();
        root.getChildren().addAll(buttons, dadiPane);

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

            public void handle(MouseEvent event) {
                    System.out.println("Adding img");
                    final ImageView img = new ImageView("2.jpg");
                    // should I put final in front of ImageView?
                    img.addEventHandler(ActionEvent.ACTION, new EventHandler<ActionEvent>() {

                        public void handle(ActionEvent event) {
                            // I want that when a tile is pressed an event occours.
                            // that event should be "add a new image to the ImageView just clicked",
                            // for example: img is not "2.jpg" but "3.jpj", by the way, I'm not able neither 
                            // to to print the folowing messagge :(
                            // It's like my root is pressed even if my mouse ha clicked at the image in img var.
                            System.out.println("Tile pressed ");
                }else System.out.println("You cannot create more than 5 dices");
     * @param args the command line arguments
    public static void main(String[] args) {

10/22/2016 11:29:50 PM

Accepted Answer

ImageViews don't generate ActionEvents; 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.


img.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {

     public void handle(MouseEvent event) {
         System.out.println("Tile pressed ");
8/29/2014 11:51:27 AM

@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 ");

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow