7

My application has a main app Stage that opens a 2nd window from it. I want to focus on only one stage.

I have 2 issues which I want to be resolved :

1 - How can I put the focus on only the second Stage (a fileChooser OpenDialog)? i.e. I can't switch to the main app Stage until user has clicked on Open or Cancel.

2 - How can I oblige the user to close the 2nd Stage before he can close the main Stage?

Right now, I can close the main window while the 2nd stage(OpenDialog) is still running.

Thanks.

ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176
Mohamed Benmahdjoub
  • 1,270
  • 4
  • 19
  • 38
  • why dont you simply pass your first stage as parameter to `showOpenDialog`? Creating a second stage only for your dialog is kinda .... useless ... – specializt Apr 08 '15 at 12:26

2 Answers2

14

You can use a combination of Modality and Ownership of stages.

subStage.initOwner(stage) -> Makes sure that the substage moves along with its owner.

subStage.initModality(Modality.WINDOW_MODAL) -> Makes sure that substage blocks input events from being delivered to all windows from its owner (parent) to its root.

You can also use Modality.APPLICATION_MODAL if you want to block input events to all windows from the same application, except for those from its child hierarchy.

Dialog follows modal and blocking fashion by default. The default modality for Dialog is Modality.APPLICATION_MODAL and you can add initOwner(...) to it.

Note: You cannot apply the above stated rules to FileChooser. However, you can use showOpenDialog(Window ownerWindow) for it.

fileChooser.showOpenDialog(stage);

Complete Example

import javafx.application.Application;
import javafx.stage.Modality;
import javafx.stage.Stage;

public class Main extends Application {

    @Override public void start(Stage stage) {

        stage.setTitle("Main Stage");
        stage.setWidth(500);
        stage.setHeight(500);
        stage.show();

        Stage subStage = new Stage();
        subStage.setTitle("Sub Stage");
        subStage.setWidth(250);
        subStage.setHeight(250);
        subStage.initOwner(stage);
        subStage.initModality(Modality.WINDOW_MODAL);
        subStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176
  • what if it's a fileChooser ? – Mohamed Benmahdjoub Apr 08 '15 at 14:02
  • You cannot apply the above stated rules to [FileChooser](http://docs.oracle.com/javase/8/javafx/api/javafx/stage/FileChooser.html). But, the [Dialog](http://docs.oracle.com/javase/8/javafx/api/index.html?overview-summary.html) follows modal and blocking fashion by default. The default modality for Dialog is `Modality.APPLICATION_MODAL` – ItachiUchiha Apr 08 '15 at 14:22
  • my filechooser doesn't do it, it allows access to the original window, can i give u the code plz ? – Mohamed Benmahdjoub Apr 08 '15 at 16:51
1

To focus only on one stage and block the others, just apply to the stage that you want it to be the only one that's active at a certain time the following :

stage.initModality(Modality.APPLICATION_MODAL);

Majd Saadaoui
  • 11
  • 1
  • 3