2018년 3월 11일 일요일

[JavaFX] Alert Dialog (Message) Box Examples



1. Alert Dialog Box








MessageBox.java
1
2
3
4
5
6
7
8
9
    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);
        alert.initModality(Modality.APPLICATION_MODAL);
        alert.showAndWait();
    } 

Controller.java
1
2
3
4
    @FXML
    private void handleAlert(ActionEvent event) {
        MessageBox.alert(stage, "Alert", "Alert Dialog Box !");
    }


2. Yes/No Dialog Box


MessageBox.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
    public static boolean yesno(Stage ownerStage, String title, String message) {
        Alert alert = new Alert(AlertType.CONFIRMATION);
        alert.setTitle(title);
        alert.setHeaderText(null);
        alert.setContentText(message);
        Optional<ButtonType> result = alert.showAndWait();
        if (result.get() == ButtonType.OK ){
            return true;
        } else {
            return false;
        }
    } 

Controller.java
1
2
3
4
5
6
7
8
    @FXML
    private void handleYesNo(ActionEvent event) {
        if( MessageBox.yesno(stage, "Alert", "Yes/No Dialog Box !") ) {
            MessageBox.alert(stage, "Alert", "Yes Clicked !");
        } else {
            MessageBox.alert(stage, "Alert", "No Clicked !");
        }
    }


3. Expandable Text Dialog Box.




MessageBox.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
26
27
28
    public static void text(Stage ownerStage, String title, String message, String text_title, String text) {
        Alert alert = new Alert(AlertType.ERROR);
        alert.setTitle(title);
        alert.setHeaderText(null);
        alert.setContentText(message);
        alert.initOwner(ownerStage);

        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        Label label = new Label(text_title);

        TextArea textArea = new TextArea(text);
        textArea.setEditable(false);
        textArea.setWrapText(true);

        textArea.setMaxWidth(Double.MAX_VALUE);
        textArea.setMaxHeight(Double.MAX_VALUE);
        GridPane.setVgrow(textArea, Priority.ALWAYS);
        GridPane.setHgrow(textArea, Priority.ALWAYS);

        GridPane expContent = new GridPane();
        expContent.setMaxWidth(Double.MAX_VALUE);
        expContent.add(label, 0, 0);
        expContent.add(textArea, 0, 1);

        alert.getDialogPane().setExpandableContent(expContent);
        alert.showAndWait();
    } 

Controller.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
    @FXML
    private void handleText(ActionEvent event) {
        String text = "Dialogue (sometimes spelled dialog in "+
        "American English[1]) is a written or spoken conversational "+
        "exchange between two or more people, and a literary and "+
        "theatrical form that depicts such an exchange. As a narrative, "+
        "philosophical or didactic device, it is chiefly associated "+
        "in the West with the Socratic dialogue as developed by Plato, "+
        "but antecedents are also found in other traditions including "+
        "Indian literature.[2]";
        MessageBox.text(stage, "Alert", "Alert Dialog with Text Message", "Text information:", text);
    }


4. Get Text Dialog Box.


MessageBox.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
    public static String getText(Stage ownerStage, String title, String message, String def_value) {
        TextInputDialog dialog = new TextInputDialog(def_value);
        dialog.setTitle(title);
        dialog.setHeaderText(null);
        dialog.setContentText(message);
        dialog.initOwner(ownerStage);

        Optional<String> result = dialog.showAndWait();
        if (result.isPresent()){
            return result.get();
        }
        return null;
    }   

Controller.java
1
2
3
4
5
6
7
    @FXML
    private void handleGetText(ActionEvent event) {
        String value = MessageBox.getText(stage, "Alert", "Input Your Name", "Zdiv");
        if( value != null ) {
            MessageBox.alert(stage, "Name", value);
        }
    }


5. Choice Dialog Box.




MessageBox.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
    public static String choice(Stage ownerStage, String title, String message, List<String> values) {
        ChoiceDialog<String> dialog = new ChoiceDialog<>(values.get(0), values);
        dialog.setTitle(title);
        dialog.setHeaderText(null);
        dialog.setContentText(message);
        dialog.initOwner(ownerStage);

        Optional<String> result = dialog.showAndWait();
        if (result.isPresent()){
            return result.get();
        }
        return null;
    } 

Controller.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
    @FXML
    private void handleChoice(ActionEvent event) {
        List<String> values = new ArrayList<>() ;
        values.add("Alice");
        values.add("Tom");
        values.add("Zdiv");
        String value = MessageBox.choice(stage, "Alert", "Select Name", values);
        if( value != null ) {
            MessageBox.alert(stage, "Name", value);
        }
    }



6. Dialog Box with Custom Button



MessageBox.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
    public static int custonButton(Stage ownerStage, String title, String message) {
        Alert alert = new Alert(AlertType.CONFIRMATION);
        alert.setTitle(title);
        alert.setHeaderText(null);
        alert.setContentText(message);
        alert.initOwner(ownerStage);

        ButtonType buttonTypeYes = new ButtonType("Yes", ButtonData.YES);
        ButtonType buttonTypeNo = new ButtonType("No", ButtonData.NO);
        ButtonType buttonTypeCancel = new ButtonType("Cancel", ButtonData.CANCEL_CLOSE);
        ButtonType buttonTypeCustom = new ButtonType("Custom");
        alert.getButtonTypes().setAll(buttonTypeYes, buttonTypeNo, buttonTypeCancel, buttonTypeCustom);

        Optional<ButtonType> result = alert.showAndWait();
        if (result.get() == buttonTypeYes){
            return 0;
        } 
        if (result.get() == buttonTypeNo) {
            return -1;
        } 
        if (result.get() == buttonTypeCancel) {
            return 1;
        }       
        return 2;
    }  

Controller.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
    @FXML
    private void handleYesNoCancel(ActionEvent event) {
        int rv = MessageBox.custonButton(stage, "Alert", "Custom Button Dialog Box !");
        switch(rv) {
            case -1: MessageBox.alert(stage, "Alert", "No Clicked !"); break;
            case 0: MessageBox.alert(stage, "Alert", "Yes Clicked !"); break;
            case 1: MessageBox.alert(stage, "Alert", "Cancel Clicked !"); break;
            case 2: MessageBox.alert(stage, "Alert", "Custom Clicked !"); break;
        }
    }



댓글 없음:

댓글 쓰기