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.
This primary stage object creates a window to display user created functionality in the output. If your requirement is more than one stage then we can create as many Stage objects as we want. JavaFX Stage class available in javafx.
The JavaFX Stage class is the top level JavaFX container. The primary Stage is constructed by the platform. Additional Stage objects may be constructed by the application. Stage objects must be constructed and modified on the JavaFX Application Thread.
Therefore, we can create multiple scenes for a given JavaFX application. In this program, we will create 2 scenes and show how to switch between the scenes in our code. So the Java code is shown below. So just like any JavaFX application, you need to import several packages.
A stage has two parameters determining its position namely Width and Height. It is divided as Content Area and Decorations (Title Bar and Borders).
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With