JAVA

자바 Dialog, modal 창 만들기

Tech Signal 2020. 7. 10. 19:00
  • HBox root = new HBox(10); //modal 윈도우를 위해선 parent 창(primary stage)가 필요하다.

임의의 창 만들기 순서

1. Stage 객체 생성 / Stage는 창 1개.

Stage dialog = new Stage(StageStyle.UTILITY);

//Utility: a Stage style with a solid white background and minimal platform decorations used for a utility window.

 

2. 모달창 여부 설정 (모달 창은 child창이 나타나면 parent창 사용할 수 없음)

dialog.initModality(Modality.APPLICATION_MODAL);

//APPLICATION_MODAL: a modal window that blocks events from being delivered to any other application 

 

3. 부모창 지정 (말은 parent지만 initializing Owner)

dialog.initOwner(primaryStage);

 

4. 자식창에 나타날 컨테이너 객체 생성 (fxml 넣어준다)

Parent parent = FXMLLoader.load(getClass().getResource("myDialog.fxml");

// try-catch 필요

 

5. parent창에서 fxml로 만든 child창의 컨트롤 객체 얻기 (입력된 요소 가져오기)

TextField name = (TextField) parent.lookup("#txtName");

PasswordField pass = (PasswordField) parent.lookup("#pass");

Button btnOk = (Button) parent.lookup("#btnOk");

 

//parent(FXMLLoader로 로드한 fxml)에서 #btnOK 등을 검색(lookup)해 그 값을 변수로 가져온다.

 

6. Scene객체 생성해서 컨테이너 객체 추가

Scene scene = new Scene(parent);

 

7. Stage객체에 Scene 객체 추가

dialog.setScene(scene);

dialog.setResizable(true); //창 크기 고정 or NOT

dialog.show();