mmik
mmik

Reputation: 5961

Show and Hide Main Stage in JavaFX

By default, when all windows are closed in JavaFX, the app terminates. However, I've used Platform.setImplicistExit(false) to let the app stays. Now, how would I show the app when the app is activated? Is there any activate event listener that I can listen to when the app is activated from the Mac Dock Bar?

@Override
public void start(final Stage stage) throws Exception {
    stage.setTitle("Hello World");

    stage.setScene(
            createScene(
                    loadMainPane()
            )
    );

    stage.show();

    Platform.setImplicitExit(false);
    stage.setResizable(false);
}

Or is there an event listen to listen when the system close button is clicked so I can hide the stage stage.close() and the app will show when is activated?

Upvotes: 2

Views: 2569

Answers (1)

See if this code is of any use. This minimises the stage upon close request instead of turning off implicit exit. You still need a way to properly close this without task manager, but this seems like what you want. If it does not just comment below.

package minimalist;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Stack_Overflow extends Application{

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

    @Override
    public void start(Stage primaryStage) throws Exception {
        TextArea text = new TextArea();
        text.setText("Hello World!");
        StackPane stack = new StackPane();
        stack.getChildren().add(text);
        Scene scene = new Scene(stack);
        primaryStage.setScene(scene);
        primaryStage.setOnCloseRequest(e -> {
            e.consume();
            primaryStage.setIconified(true);
        });
        primaryStage.show();
    }
}

Upvotes: 3

Related Questions