JavaFX reach created object in same view

I got pane where when it loads, it creates rectangles from file (each object is reactangle). That object saves information about rectangles X/Y etc. When it’s created I give that rectangle ID to map with hashmap (HashMap<Integer, Radar>) Integer is (rectangle.setId(i.toString());). So base problem here is that after I’m done dragging rectagles I want to save theirs current location. For that reason I tried using

Rectangle rectangle = (Rectangle) pane.lookup(i.toString());

but it just returns null

public class MainWindow {

    @FXML
    private MenuItem menuItemSave;

    @FXML
    private AnchorPane pane;

    private ViewManager viewManager;
    private static final int cubeSize = 10;
    private HashMap<Integer, Radar> radarHashMap;

    public void initManager(ViewManager viewManager) {
        this.viewManager = viewManager;
        loadRadars();
    }

    private double orgSceneX, orgSceneY;
    private double orgTranslateX, orgTranslateY;
    EventHandler<MouseEvent> rectangleOnMousePressedEventHandler =
            new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent t) {
                    orgSceneX = t.getSceneX();
                    orgSceneY = t.getSceneY();
                    orgTranslateX = (((Rectangle) (t.getSource())).getTranslateX());
                    orgTranslateY = (((Rectangle) (t.getSource())).getTranslateY());
                }
            };


    EventHandler<MouseEvent> rectangleOnMouseDraggedEventHandler =
            new EventHandler<MouseEvent>() {

                @Override
                public void handle(MouseEvent t) {
                    double offsetX = t.getSceneX() - orgSceneX;
                    double offsetY = t.getSceneY() - orgSceneY;
                    double newTranslateX = orgTranslateX + offsetX;
                    double newTranslateY = orgTranslateY + offsetY;
                    ((Rectangle) (t.getSource())).setTranslateX(newTranslateX);
                    ((Rectangle) (t.getSource())).setTranslateY(newTranslateY);
                }
            };

    private void loadRadars() {
        radarHashMap = updateCourses();
        for (Integer i : radarHashMap.keySet()) {
            Rectangle rectangle = new Rectangle(radarHashMap.get(i).getX(), radarHashMap.get(i).getY(), cubeSize, cubeSize);
            rectangle.setFill(Color.YELLOW);
            rectangle.setCursor(Cursor.HAND);
            rectangle.setId(i.toString());
            rectangle.setOnMousePressed(rectangleOnMousePressedEventHandler);
            rectangle.setOnMouseDragged(rectangleOnMouseDraggedEventHandler);
            pane.getChildren().add(rectangle);
        }
    }

    @FXML
    void SaveChanges(ActionEvent event) {
        for (Integer i : radarHashMap.keySet()) {
            Rectangle rectangle = (Rectangle) pane.lookup(i.toString());
            System.out.println(rectangle.getId());
            System.out.println(rectangle.getY());
        }
    }
}

Answer

Rectangle rectangle = (Rectangle) pane.lookup(“#”+i.toString());

Leave a Reply

Your email address will not be published. Required fields are marked *