[英]JavaFX 8 Create Undecorated TextInputDialog that behvaes as modal over full screen stage
我有一個全屏主應用程序階段,並希望使 TextInputDialog 出現在頂部,具有模態樣式(即在向舞台提供其他輸入之前必須關閉對話框)。
如果我將所有者設置為舞台,並將樣式設置為 UTILITY,則它可以工作。
但是,如果我想刪除裝飾(通過將樣式設置為 UNDECORATED),則該對話框根本不會出現! 我只能通過不設置父級來讓它出現。
如果我不設置父級,對話框會出現在舞台的頂部,但如果我點擊應用程序上的任何其他地方,它就會消失在全屏舞台的后面。
如何創建一個在全屏舞台上具有類似模態行為的未修飾對話框?
// This works, but is decorated
TextInputDialog dialog = new TextInputDialog();
dialog.initOwner(stage);
dialog.initStyle(StageStyle.UTILITY);
dialog.showAndWait();
// This doesn't show at all (probably appears underneath the stage?)
TextInputDialog dialog = new TextInputDialog();
dialog.initOwner(stage);
dialog.initStyle(StageStyle.UNDECORATED);
dialog.showAndWait();
// This still allows clicking in the stage while dialog shown. Also causes
// "exit" of fullscreen by showing task bar when dialog is shown
TextInputDialog dialog = new TextInputDialog();
dialog.initStyle(StageStyle.UNDECORATED);
dialog.showAndWait();
我已經做了一個完整的例子,這似乎是你嘗試過的東西,但沒有用。 就我而言,它雖然很好用。 窗口顯示全屏,對話框出現在沒有窗口裝飾的中心。
public class DialogCheck extends Application{
public static void main(String[] args){
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("text field");
BorderPane root = new BorderPane();
Button b = new Button("dialog");
root.setCenter(b);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setFullScreen(true);
primaryStage.show();
b.setOnAction((evt)->{
TextInputDialog dialog = new TextInputDialog();
dialog.initOwner(primaryStage);
dialog.initStyle(StageStyle.UNDECORATED);
dialog.showAndWait();
});
}
}
請注意,這是在 osx 上使用 java 11 和 openjfx 11。
對於一些額外的調試:
dialog.setOnShown( sevt ->{
System.out.println("showing: " + dialog.isShowing() + ", " + dialog.getX());
TextField field = dialog.getEditor();
field.requestFocus();
System.out.println(field.isFocused());
});
還要注意使用“showAndWait”和“show”的區別。
Dialog.showAndWait
輸出:
顯示:假,NaN。
錯誤的
Dialog.show
輸出:
顯示:真實,621.0
真的
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.