如何从java启动应用程序并将启动的应用程序保持在当前应用程序之上

如何从java启动应用程序并将启动的应用程序保持在当前应用程序之上,java,javafx,Java,Javafx,我有一个javaFX应用程序,它基本上是一个门户。它包含可用于启动各种其他应用程序的按钮 简化的可测试版本为: public class PortalApplication extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) {

我有一个javaFX应用程序,它基本上是一个门户。它包含可用于启动各种其他应用程序的按钮

简化的可测试版本为:

public class PortalApplication extends Application {

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

    @Override
    public void start(Stage primaryStage) {
        FlowPane root = new FlowPane();
        primaryStage.setScene(new Scene(root));
        primaryStage.setMaximized(true);
        primaryStage.show();

        root.getChildren().add(createButton("Launch 'Notepad'", "notepad"));
        root.getChildren().add(createButton("Launch 'My App'", "java -jar app1.jar"));
        //..

    }

    private Button createButton(String title, String command) {
        Button button = new Button(title);
        button.setOnAction(event -> {
            button.setDisable(true);
            new Thread(() -> {
                try {
                    Thread.sleep(1000); //simulated delay
                    Runtime.getRuntime().exec(command);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
        });
        return button;
    }
}
问题:有时用户启动应用程序,然后在应用程序加载或运行时再次在门户中单击,导致新启动的应用程序失去焦点并落后于门户应用程序。大多数用户没有意识到这一点,认为启动的应用程序没有启动

如何启动应用程序,使它们始终位于门户应用程序的顶部

我认为一个解决方案是,每次门户应用程序获得焦点时,我都可以通过编程将焦点转移到已启动的应用程序(如果它存在的话),但我没有找到一种方法来做到这一点

看看它是否有帮助