Dialog
Dialog
Dialog<ButtonType> dialog = new Dialog<>();
dialog.setTitle("Title");
dialog.setHeaderText("HeaderText");
dialog.setContentText("ContentText");
ImageView imageView = new ImageView("image\\portrait.jpg");
imageView.setFitWidth(50);
imageView.setPreserveRatio(true);
dialog.setGraphic(imageView);
DialogPane dialogPane = dialog.getDialogPane();
dialogPane.setPrefSize(500, 300);
ObservableList<ButtonType> buttonTypes = dialogPane.getButtonTypes();
buttonTypes.addAll(ButtonType.OK, ButtonType.APPLY, ButtonType.CANCEL);
Button btnOk = (Button) dialogPane.lookupButton(ButtonType.OK);
Button btnApply = (Button) dialogPane.lookupButton(ButtonType.APPLY);
Button btnCancel = (Button) dialogPane.lookupButton(ButtonType.CANCEL);
btnOk.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("OK");
}
});
btnApply.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Apply");
}
});
btnCancel.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Cancel");
}
});
dialog.setOnCloseRequest(new EventHandler<DialogEvent>() {
@Override
public void handle(DialogEvent event) {
System.out.println("OnCloseRequest");
}
});
/*
dialog.showAndWait()
.filter(response -> response == ButtonType.OK)
.ifPresent(response -> System.out.println("doAction"));
*/
dialog.showAndWait().filter(new Predicate<ButtonType>() {
@Override
public boolean test(ButtonType buttonType) {
return buttonType == ButtonType.OK;
}
}).ifPresent(new Consumer<ButtonType>() {
@Override
public void accept(ButtonType buttonType) {
System.out.println("doAction");
}
});
System.out.println("阻塞的代码");
dialog.show();
System.out.println("不会阻塞的代码");
//如果 Dialog<> 泛型不是ButtonType 需要设置 ResultConverter
//dialog.setResultConverter(new Callback<ButtonType, ButtonType>() {
// @Override
// public String call(ButtonType param) {
// return "";
// }
//});
设置左上角小图标
//设置左上角小图标
Stage stage = (Stage) dialog.getDialogPane().getScene().getWindow();
stage.getIcons().add(new Image("image/stash.png"));
//使用与它正在运行的应用程序相同的图标
dialog.initOwner(primaryStage);
dialog.initStyle(StageStyle.UTILITY);
Alert
Alert alert = new Alert(AlertType.WARNING);
alert.setTitle("Title");
alert.setHeaderText("HeaderText");
alert.setContentText("ContentText");
Button btnOk = (Button) alert.getDialogPane().lookupButton(ButtonType.OK);
btnOk.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Ok");
}
});
alert.show();
ChoiceDialog
ObservableList<String> itemStrings = FXCollections.observableArrayList();
for (int i = 0; i < 6; i++) {
itemStrings.add("item " + i);
}
ChoiceDialog<String> choiceDialog = new ChoiceDialog<>();
choiceDialog.setSelectedItem("item 3");
choiceDialog.getItems().addAll(itemStrings);
Button btnOk = (Button) choiceDialog.getDialogPane().lookupButton(ButtonType.OK);
btnOk.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Ok");
}
});
choiceDialog.selectedItemProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
}
});
TextInputDialog
TextInputDialog textInputDialog = new TextInputDialog();
textInputDialog.setTitle("Title");
textInputDialog.setHeaderText("HeaderText");
textInputDialog.setContentText("ContentText");
Button btnOk = (Button) textInputDialog.getDialogPane().lookupButton(ButtonType.OK);
btnOk.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
//输入框中的文本
String text = textInputDialog.getEditor().getText();
System.out.println(text);
}
});
//设置左上角小图标
Stage stage = (Stage) textInputDialog.getDialogPane().getScene().getWindow();
stage.getIcons().add(new Image("image/stash.png"));
textInputDialog.show();