2017-02-12 14 views
0

ステージをポップアップの所有者として設定しようとしていますが、所有者がアプリケーションのメインステージでない場合はポップアップは表示されません。所有者ウィンドウが表示されていない場合、JavaFXポップアップは表示されません。

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

} 

答えて

1

あなたがポップアップを表示する前に、あなたの子供のステージにshow(...)を呼び出す必要があります。

クラスJanelaModalを使用して、簡単なテストJava FXアプリケーションを作成しました。これは、メインクラス

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

であり、これは

The test application

ポップアップが「子」のステージに示されている結果です。あなたは

popup(modal, "Test message"); 

を呼び出すしかし、あなたはポップアップ(ステージ)

modal.show(); 

を呼び出さない場合は表示されません。

私は、何とか助けてくれました。

+1

お返事ありがとうございます。ポップアップが表示されたら、所有者ウィンドウが表示されている必要があります。私の場合、子ウィンドウは閉じられているので、メインウィンドウはポップアップの所有者でなければなりません。なぜドキュメンテーションにそれが言及されていないのだろうか、それはあまり明らかではない。 – ceklock

関連する問題