JavaFX 8 How to set program icon to alert?

前端 未结 4 786
温柔的废话
温柔的废话 2021-01-19 01:29

How can I set program icon to alert without using alert.initOwner()? Why without initOwner? It\'s because some alert must be shown before whole win

相关标签:
4条回答
  • 2021-01-19 02:09

    This is how it is done:

    // Get the Stage.
    Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
    
    // Add a custom icon.
    stage.getIcons().stage.getIcons().add(new Image("images/logo_full3.png"));
    

    There might be problem with the image reference above. But you can try to configure as long as it works. This is how I do (I use maven). Yours might be different if you are not using maven.

    Full tutorial here: Alert javafx tutorial

    0 讨论(0)
  • 2021-01-19 02:14

    The correct implementation is following comments above:

    // Get the Stage.
    Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
    
    // Add a custom icon.
    stage.getIcons().add(new Image(getClass().getResourceAsStream("images/logo_full3.png")));
    
    0 讨论(0)
  • 2021-01-19 02:20

    You can steal the DialogPane from an Alert instance, and add it to a regular Stage. A Node can only be the root of one Scene at a time, so you need to replace the root of the Alert’s Scene first:

    public class AlertWithIcon
    extends Application {
        @Override
        public void start(Stage stage) {
            Alert alert = new Alert(Alert.AlertType.CONFIRMATION,
                "Are you sure you want to delete this item?",
                ButtonType.YES, ButtonType.NO);
            alert.setHeaderText("Delete Item");
    
            DialogPane pane = alert.getDialogPane();
    
            ObjectProperty<ButtonType> result = new SimpleObjectProperty<>();
            for (ButtonType type : pane.getButtonTypes()) {
                ButtonType resultValue = type;
                ((Button) pane.lookupButton(type)).setOnAction(e -> {
                    result.set(resultValue);
                    pane.getScene().getWindow().hide();
                });
            }
    
            pane.getScene().setRoot(new Label());
            Scene scene = new Scene(pane);
    
            Stage dialog = new Stage();
            dialog.setScene(scene);
            dialog.setTitle("Delete Item");
            dialog.getIcons().add(new Image("GenericApp.png"));
    
            result.set(null);
            dialog.showAndWait();
    
            System.out.println("Result is " + result);
        }
    }
    
    0 讨论(0)
  • 2021-01-19 02:33
    public class AlertWithIcon
    extends Application {
        @Override
        public void start(Stage stage) {
            Alert alert = new Alert(Alert.AlertType.CONFIRMATION,
                "Are you sure you want to delete this item?",
            ButtonType.YES, ButtonType.NO);    
           alert.setHeaderText("Delete Item"); 
       ((Stage)alert.getDialogPane().getScene().getWindow()).getIcons().add(new image("GenericApp.png"));
        alert.showAndWait();
    }
    }
    
    0 讨论(0)
提交回复
热议问题