Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

javafx

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 {

@Override
public void start(Stage primaryStage) {
    CustomPane root = new CustomPane();
    root.setPadding(new Insets(20));
    root.setHgap(10);
    root.setVgap(10);
    for (int i = 0; i < 10; i++) {
        RectangleBox recB = new RectangleBox();
        root.getChildren().add(recB);
    }

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

    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show();
}


public static void main(String[] args) {
    launch(args);
}

}

class RectangleBox extends VBox {

static int index = 0;

public RectangleBox() {
    Rectangle rec = new Rectangle(150, 150);
    rec.setFill(Color.GREEN);

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

        @Override
        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).

        }
    });

    cm.getItems().add(removeRec);
    createContextMenuEvent(cm, rec);

}

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

        @Override
        public void handle(MouseEvent t) {
            if (t.getButton() == MouseButton.SECONDARY) {
                cm.show(rec, t.getScreenX(), t.getScreenY());
            }
        }
    });
}

}

class CustomPane extends FlowPane {

public CustomPane() {
    //setAlignment(Pos.CENTER);
    setHgap(25);
    setVgap(25);
    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.

like image 678
Dess Avatar asked Jan 03 '14 20:01

Dess


1 Answers

try this:

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

hope it helps.

like image 78
doomsdaymachine Avatar answered Oct 12 '22 09:10

doomsdaymachine