How to detect Node at specific point in JavaFX?

2019-02-25 01:20发布

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());
            }

        }
    }

};  

}

0条回答
登录 后发表回答