bog
bog

Reputation: 1323

javafx how to separate events of a StackPane and events of an element contained?

I have a StackPane containing several elements (Rectangles). I added an ActionEventHandler to all of them. I expect that when Rectangle object is clicked it starts anly the event added to its. Instead I see that both events are started, like I have pressed Rectangle and StackPane together. Here's the code:

/*
 * 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 provapanetile;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.TilePane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

/**
 *
 * @author Ambra
 */
public class ProvaPaneTile extends Application {

    @Override
    public void start(Stage primaryStage) {       

        StackPane root = new StackPane();
        TilePane tp = new TilePane();
        tp.setVgap(10);
        tp.setHgap(10);

        for(int i = 0; i<5; i++){
            tp.getChildren().add(new Rectangle(50, 50, Color.AZURE));
        }            
        addListners(root, tp);
        root.getChildren().add(tp);

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

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    public void addListners(StackPane sp, TilePane tp){
        sp.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                System.out.println("You pressed StackPointer");
            }
        });
        tp.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                System.out.println("You pressed TilePane");
            }
        });
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

Clicking ones on a Rectangle I get this 2 messagges:

You pressed TilePane
You pressed StackPointer

Upvotes: 0

Views: 933

Answers (1)

James_D
James_D

Reputation: 209358

Consume the event in the handler on the tile pane:

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

        @Override
        public void handle(MouseEvent event) {
            System.out.println("You pressed TilePane");
            event.consume();
        }
    });

Now when you click on the TilePane, only the handler registered with the tile pane will be invoked.

Note that in this example, the TilePane entirely covers the StackPane, so it's impossible to actually invoke the handler on the StackPane, which is probably not what you want. To fix that, you would need to manage the layout so that the TilePane did not grow to the full size of the StackPane; but obviously this is just an example to demonstrate the issue so perhaps this is enough for your real application...

Upvotes: 0

Related Questions