0

I have an Hbox into which I upload images (more than one) in a loop. I'm trying to figure out their coordinates, but the layoutX of each of them = 0, as if they are at the same point (although everything is fine in the application as a result). Moreover, if after some interaction you add an image to another hbox, then its layout is displayed normally.

    for (Card card:data.getCards()) {
        ImageView imageView=new ImageView("imageSrc");
        hbox.getChildren().add(imageView);
        System.out.println(imageView.localToScene(new Point2D(0,0)));
        System.out.println(imageView.getLayoutX());
    }

result:

Point2D [x = 124.0, y = 225.0]

0.0

Point2D [x = 124.0, y = 225.0]

0.0

Point2D [x = 124.0, y = 225.0]

0.0

how do I determine the object's coordinate in this case?

  • Is `hbox` part of a Scene when you call `localToScene`? – VGR Jul 30 '22 at 14:09
  • Yes, hbox is part – Oleg Boldov Jul 30 '22 at 14:31
  • 2
    You’re trying to access the layout coordinates before the hbox has been laid out. You can try calling `layout()` before accessing the coordinates, but this seems like an x-y problem. You should probably find a way to do whatever it is you’re trying to do that doesn’t need those coordinates (typically by using an appropriate layout pane that does the layout for you). – James_D Jul 30 '22 at 15:03
  • Using an HBox or some other managed layout region and then peeking in to see where things are in X/Y coordinates is usually some kind of a design smell. What are you going to do with this knowledge? Maybe you'd be better off using Pane and manually placing everything - then it won't move around on you automatically. – DaveB Jul 31 '22 at 15:37

0 Answers0