如何在 JavaFX 中的特定点获取节点?
在 Java Swing 中,有一个方法SwingUtilities.getDeepestComponentAt
。是否有任何支持 Swing 的 JavaFX 方法?
在 JavaFx 中,
javafx.scene.Node.impl_pickNode(double parentX, double parentY)
但它返回null
代码:
scene.getRoot().impl_pickNode(428.0, 278.0);
例子:
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());
}
}
}
};
}