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