3

How to get the Node at specific point in JavaFX?
In Java Swing, there is a method SwingUtilities.getDeepestComponentAt. Is there any JavaFX method that support as Swing?

In JavaFx, javafx.scene.Node.impl_pickNode(double parentX, double parentY) But it returns null

Sinppet code:

scene.getRoot().impl_pickNode(428.0, 278.0);

Example:

public class Main extends Application {

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

@Override
  public void start(Stage stage) {
    stage.setTitle("ComboBoxSample");
    Scene scene = new Scene(new Group(), 450, 250);

    TextField notification = new TextField ();
    notification.setText("Label");

    notification.clear();

    GridPane grid = new GridPane();
    grid.setVgap(4);
    grid.setHgap(10);
    grid.setPadding(new Insets(5, 5, 5, 5));
    grid.add(new Label("To: "), 0, 0);
    grid.add(notification, 1, 0);

    scene.setOnMouseClicked(mouseHandler);

    Group root = (Group) scene.getRoot();
    root.getChildren().add(grid);
    stage.setScene(scene);
    stage.show();

  }

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

    @Override
    public void handle(MouseEvent mouseEvent) {
        System.out
                .println(mouseEvent.getEventType() + "\n" + "X : Y - "
                        + mouseEvent.getX() + " : " + mouseEvent.getY()
                        + "\n" + "SceneX : SceneY - "
                        + mouseEvent.getSceneX() + " : "
                        + mouseEvent.getSceneY() + "\n"
                        + "ScreenX : ScreenY - " + mouseEvent.getScreenX()
                        + " : " + mouseEvent.getScreenY());
        Scene scene = (Scene) mouseEvent.getSource();
        Object node =  scene.getRoot().impl_pickNode(428.0, 278.0);
        if(node != null)
            System.out.println("node is: " + node.getClass().getName().toString());
        else{
            System.out.println("node is NULL");
        }




        Iterator<Window> windows = Window.impl_getWindows();
        while (windows.hasNext()) {
            Window wnd = windows.next();

            if (wnd instanceof Stage) {
                Stage stage = (Stage) wnd;
                System.out.println("---> " + stage.getTitle());
            }

        }
    }

};  

}

Phuc Thai
  • 718
  • 7
  • 17
  • I used this method, and it didn't return null, but some valueable node. Possibly, smth is wrong with coordinates determining, on your side. Because I used it in the same way. And it looks like, it cannot work in a wring way, because seems it is used for internal purposes – Alexander Kirov Mar 12 '13 at 07:54
  • The root is an empty `parent` ? – gontard Mar 12 '13 at 08:16
  • Bin, could you provide a sample? – Alexander Kirov Mar 12 '13 at 09:08
  • possible duplicate of [JavaFX 2.2 get node at coordinates (visual tree hit testing)](http://stackoverflow.com/questions/14420569/javafx-2-2-get-node-at-coordinates-visual-tree-hit-testing) – jewelsea Mar 12 '13 at 21:40
  • if you can upgrade to java 8 the method impl_pickNode was improved there – Marcel Oct 27 '15 at 13:06

0 Answers0