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);
}
}