2017-06-21 13 views
0

http://code.makery.ch/blog/javafx-dialogs-official/階段顯示瞭如何獲得一個JavaFX的8對話框的階段:未能得到一個對話框

// Get the Stage. 
Stage stage = (Stage) dialog.getDialogPane().getScene().getWindow(); 

唉,這並沒有爲我工作:對話窗格(雖然顯示)給出null.getScene()

有沒有其他簡單的方法來獲得舞臺或至少是一個開放的對話窗口的場景?

問題的背景是,在某些情況下,需要向用戶顯示Alert,同時保持底層對話框窗口打開。目前,由於Modality值的組合無效,所以不起作用,但這是一個不同的主題。

+1

適合我。你可以發佈[MCVE]嗎? –

回答

1

很難肯定地說,如果您發佈沒有上下文代碼,但我認爲問題是時機。在showAndWait之前(或至少在對話框關閉之前),您需要獲得舞臺。試試這個:

public static boolean showConfirmationDialog(String contentText, String headerText) { 
    Alert alert = new Alert(Alert.AlertType.CONFIRMATION, contentText, ButtonType.YES, ButtonType.NO); 
    alert.setTitle("Test"); 
    alert.setHeaderText(headerText); 

    Window alertWindow = alert.getDialogPane().getScene().getWindow(); 
    System.out.println("alertWindow.getOpacity(): " + alertWindow.getOpacity()); 

    Optional<ButtonType> result = alert.showAndWait(); 

    //This would cause a NullPointerException at this point: 
    //alertWindow = alert.getDialogPane().getScene().getWindow(); 
    //System.out.println("alertWindow.getOpacity(): " + alertWindow.getOpacity()); 

    return (result.get() == ButtonType.YES); 
} 
+0

問題的確是時機。測試發生在result屬性的更改偵聽器中,並且僅在對話框關閉後才觸發。感謝您的幫助! – glglgl

相關問題