본문 바로가기

JavaFX

[JavaFX] 자바fx 화면 생성과 동시에 팝업(모달) 띄우기 Platform.runLater

자바fx로 개발하면서 화면이 생성됨과 동시에 팝업을 띄워야 할때가 있다.

그냥 팝업만 띄우면 상관 없지만 팝업을 띄운 후 부모창을 선택할 수 없게 하려고 한다면

initOwer을 사용하면 될것이다.

하지만 initOwer을 사용하려면 부모창의 scene을 넘겨줘야 하는데,

fxml로 화면을 생성하고 fxml에서 anchor등의 객체를 가져오기 전에 팝업을 호출하기 때문에 에러가 발생한다.

이때 사용할 수 있는 방법이

Platform.runLater 이다.

 

 

@FXML private AnchorPane anchor;


private void popup(){
  Platform.runLater(new Runnable(){
    @Override
    public void run(){
      try{
        Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("경로");
        stage = new Stage();

        stage.initOwner(anchor.getScene().getWindow());
        stage.initModality(Modality.WINDOW_MODAL);
      } catch(Exception e){

      }
    }
  });
}