JavaFx removing from pane object when I am in this object's class


I don't know if anyone could understand my problem from the title, but here's more specific description. I have class, in which I created a FlowPane, where I added objects of another class(images packed inside VBoxes). Each VBox have ContextMenu, where is MenuItem "Remove File". My problem is, how to remove this object while beeing inside the VBox class. Here is a little part of my code:

//removed, entire code is below after edit

The code where I'm accessing my CustomPane (my class of FlowPane, with specified attributes) works, because I can remove object if I'm doing it by their indexes, but when I remove one of them, other's indexes changes, so I'm looking for another solution. I need to specifically remove the object of the class in the code.

Okay so here is so called sscce, which of I had no idea, since now:

package sscce;

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Sscce extends Application {

public void start(Stage primaryStage) {
    CustomPane root = new CustomPane();
    root.setPadding(new Insets(20));
    for (int i = 0; i < 10; i++) {
        RectangleBox recB = new RectangleBox();

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

    primaryStage.setTitle("Hello World!");

public static void main(String[] args) {


class RectangleBox extends VBox {

static int index = 0;

public RectangleBox() {
    Rectangle rec = new Rectangle(150, 150);

    StackPane sp = new StackPane();
    Label label = new Label(Integer.toString(index));
    final ContextMenu cm = new ContextMenu();
    MenuItem removeRec = new MenuItem("Remove Rectangle");
    removeRec.setOnAction(new EventHandler<ActionEvent>() {

        public void handle(ActionEvent t) {

            ((CustomPane) getParent()).getPane().getChildren().remove(0); //here is the problem, I want this line to remove the rectangle I clicked on(now it's removing first element in the pane).


    createContextMenuEvent(cm, rec);


private void createContextMenuEvent(final ContextMenu cm, final Rectangle rec) {
    addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {

        public void handle(MouseEvent t) {
            if (t.getButton() == MouseButton.SECONDARY) {
      , t.getScreenX(), t.getScreenY());


class CustomPane extends FlowPane {

public CustomPane() {
    setPadding(new Insets(20));

public CustomPane getPane() {
    return this;

It should work after copy/paste this entire code to java project. So I removed everything that is not neccessary, and I have replaced images to rectangles, now this program looks kind of stupid;p I added comment to a line I have problem with. I hope now it's a lot clearer than before.

1/3/2014 10:38:17 PM

Accepted Answer

try this:

((CustomPane) RectangleBox.this.getParent()).getChildren().remove(RectangleBox.this);

hope it helps.

1/4/2014 12:23:55 AM

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