Reputation: 6352
Trying to set a Stage as owner of a Popup, but the Popup does not appear if the owner is not the main Stage of the application.
public void popup(Window owner, String mensagem) {
Popup popup = new Popup();
popup.setAutoHide(true);
popup.setHideOnEscape(true);
Label label = new Label(mensagem);
label.setBackground(new Background(new BackgroundFill(Color.CORNSILK, null, null)));
popup.getContent().add(label);
popup.setOnShown((event) -> {
FadeTransition fade = new FadeTransition(Duration.seconds(5), label);
fade.setOnFinished((e) -> {
popup.hide();
});
fade.play();
});
popup.show(owner);
}
The child Stage:
public class JanelaModal extends Stage {
public JanelaModal(String title) {
super(StageStyle.DECORATED);
setTitle(title);
initOwner(Main.getInstance().getStage());
initModality(Modality.APPLICATION_MODAL);
setResizable(false);
}
public void setGui(Parent gui) {
if (getScene() != null) {
getScene().setRoot(gui);
} else {
setScene(new Scene(gui));
}
}
}
Upvotes: 0
Views: 1664
Reputation: 1358
You must call show(...)
on your child stage before showing the popup.
Using your class JanelaModal
I wrote a simple test Java FX application. This is the Main class
public class Main extends Application {
private static Main instance;
private Stage stage;
@Override
public void start(Stage primaryStage) throws Exception{
instance = this;
this.stage = stage;
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
//primaryStage.show(); //Don't show main stage
JanelaModal modal = new JanelaModal("TEST");
modal.setWidth(300);
modal.setHeight(300);
modal.show();
popup(modal, "Test message");
}
//Copied and pasted from the question post
public void popup(Window owner, String mensagem) {
Popup popup = new Popup();
popup.setAutoHide(true);
popup.setHideOnEscape(true);
Label label = new Label(mensagem);
label.setBackground(new Background(new BackgroundFill(Color.CORNSILK, null, null)));
popup.getContent().add(label);
popup.setOnShown((event) -> {
FadeTransition fade = new FadeTransition(Duration.seconds(5), label);
fade.setOnFinished((e) -> {
popup.hide();
});
fade.play();
});
popup.show(owner);
}
public static void main(String[] args) {
launch(args);
}
public static Main getInstance() {
return instance;
}
public Stage getStage() {
return stage;
}
}
And this is the result
The popup is shown in the "child" Stage. If you call
popup(modal, "Test message");
But you don't call
modal.show();
The popup (and the stage) will not show.
Hope I helped, somehow :)
Upvotes: 2