2018년 3월 10일 토요일

[JavaFX] Alert Dialog in JavaFX (다이얼로그 박스)

JavaFX에서 Alert Dialog는 Alert() API를 이용하여 생성할 수 있으며, 크게 다음과 같은 정보를 설정할 수 있다.
1. Alert Dialog Window Icon 설정
2. Alert Dialog Title 설정
3. Alert Type 설정
4. Message Header 설정
5. Message Content 설정
6. Dialog Modality 설정

1. Alert Dialog Window Icon 설정

메인 윈도우 창에 설정한 아이콘을 stage 정보에서 가져와서 설정한다.
Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
stage.getIcons().add(ownerStage.getIcons().get(0));

또는 리소스 파일에서 직접 지정하여 생성할 수도 있다.
Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
stage.getIcons().add(BloggerPostSnippet.class.getResourceAsStream("android.png"));

2. Alert Dialog Title 설정
alert.setTitle(title);

3. Alert Type 설정
Alert alert = new Alert(AlertType.INFORMATION);

다음 5가지의 AlertType이 있으며, Type에 따라 Alert 창에 표시되는 아이콘이 달라진다.


4. Message Header 설정
alert.setHeaderText(null);

메시지 헤더는 부가적으로 표시할 정보가 있는 경우에 추가할 수 있으며, 본문의 내용만으로 충분한 경우에는 null을 지정하여, 헤더가 표시 되지 않도록 할 수 있다.

5. Message Content 설정
alert.setContentText(message);

6. Dialog Modality 설정
1. Modality.NONE
2. Modality.WINDOW_MODAL
3. Modality.APPLICATION_MODAL

Modality는 Dialog가 팝업되어 있는 경우 다른 윈도우를 사용할 수 있는지의 여부를 결정하며, Modality가 설정되어 있으면 Blocking 모드로 동작하여 Dialog가 사라지기 전까지 다른 윈도우를 사용할 수 없게 된다. Alert() 의 디폴트 모드는 APPLICATION_MODAL이며, 이 경우 해당 Applicaiton에만 Modality가 적용되어 다른 응용프로그램은 사용할 수 있다. 만일 WINDOW_MODAL로 지정하는 경우에는 해당 Alert Dialog가 사라지기 전까지는 전체 윈도우 시스템에 대해 Modality가 적용된다.



MessageBox.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
package bloggerpostsnippet;

import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.stage.Stage;

public class MessageBox {
    public static void alert(Stage ownerStage, String title, String message) {
        Alert alert = new Alert(AlertType.INFORMATION);
        alert.setTitle(title);
        alert.setHeaderText(null);
        alert.setContentText(message);
        alert.initOwner(ownerStage);
        Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
        stage.getIcons().add(ownerStage.getIcons().get(0));
        alert.showAndWait();
    } 
}

FXMLDocumentController.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package bloggerpostsnippet;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.stage.Stage;

public class FXMLDocumentController implements Initializable {
    private Stage stage;
    @FXML
    private Label label;
    public void setStage(Stage stage) {
        this.stage = stage;
    }
    @FXML
    private void handleButtonAction(ActionEvent event) {
        MessageBox.alert(stage, "Alert", "Show Me Clicked !");
    }
    @Override
    public void initialize(URL url, ResourceBundle rb) {
    }    
}

BloggerPostSnippet.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package bloggerpostsnippet;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;

public class BloggerPostSnippet extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        Image icon = new Image(BloggerPostSnippet.class.getResourceAsStream("android.png"));
        stage.getIcons().add(icon);
        stage.setTitle("Blogger Post Snippet");
        FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));
        stage.setScene(new Scene(loader.load()));      
        FXMLDocumentController controller = loader.getController();
        controller.setStage(stage);     
        stage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }
}

Result:



댓글 없음:

댓글 쓰기