package test; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.input.MouseEvent; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.stage.Stage; public class SwitchScreensTest extends Application { Group container = new Group(); public SwitchScreensTest() { } public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage stage) { final Rectangle rect = new Rectangle(100, 30, Color.RED); rect.setTranslateY(20); final Button button = new Button("Button"); button.setTranslateY(20); container.getChildren().add(rect); Button switchBtn = new Button("switch to button and back"); switchBtn.setTranslateY(60); switchBtn.setOnAction(new EventHandler() { @Override public void handle(ActionEvent arg0) { container.getChildren().set(0, button); container.getChildren().set(0, rect); } }); Button showBtnsBtn = new Button("show button"); showBtnsBtn.setTranslateY(100); showBtnsBtn.setOnAction(new EventHandler() { @Override public void handle(ActionEvent arg0) { container.getChildren().set(0, button); } }); Scene scene = new Scene(new Group(container, switchBtn, showBtnsBtn), 300, 200); scene.setOnMouseClicked(new EventHandler() { @Override public void handle(MouseEvent event) { System.out.println(button.getBoundsInParent()); } }); stage.setScene(scene); stage.show(); } }