我已创建此方法进行电子邮件验证问题是显示的警报位于前一个fxml的顶部,而不是相同的位置,并且用户必须再次填写所有字段。
我有此方法
public boolean validateEmail() { Pattern p = Pattern.compile("[a-zA-Z0-9][a-zA-Z0-9._]*@[a-zA-Z0-9]+([.][a-zA-Z]+)+"); Matcher m = p.matcher(emailField.getText()); if (m.find() && m.group().equals(emailField.getText())) { return true; } else { Alert alert = new Alert(Alert.AlertType.WARNING); alert.setTitle("Validation of Email"); alert.setHeaderText(null); alert.setContentText("Please enter a valid Email"); alert.showAndWait(); return false; } }
我有一个按钮,onAction调用下面的方法
public void showSubscription() throws IOException { Dialog<ButtonType> dialog = new Dialog<>(); dialog.setTitle("New Subscription"); dialog.setHeaderText("Please Fulfill your information to subscribe"); FXMLLoader fxmlLoader = new FXMLLoader(); fxmlLoader.setLocation(getClass().getResource("Registration.fxml")); try { dialog.getDialogPane().setContent(fxmlLoader.load()); } catch (IOException e) { System.out.println("Couldn't load the Dialog"); e.printStackTrace(); return; } dialog.getDialogPane().getButtonTypes().add(ButtonType.OK); dialog.getDialogPane().getButtonTypes().add(ButtonType.CANCEL); Optional<ButtonType> result = dialog.showAndWait(); if (result.isPresent() && result.get()==ButtonType.OK) { RegistrationController controller = fxmlLoader.getController(); if (controller.validateEmail()) { controller.loadRegistration(); Alert alert = new Alert(Alert.AlertType.INFORMATION); alert.setTitle("Information"); alert.setHeaderText(null); alert.setContentText("Subscription Done Correctly"); alert.showAndWait(); } } else { System.out.println("CANCEL pressed"); } }
我不知道要添加什么内容以使此Alert弹出窗口在同一Registration.fxml中,而不是返回到上一个。
我已经创建了用于电子邮件验证的此方法,问题是显示的警报位于前一个fxml的顶部,而不是相同的位置,并且用户必须再次填写所有字段I ...
Alert
从initOwner(Window)
继承Dialog
方法。因此,您可以调用initOwner(...)
并进入负责显示对话框的窗口。没有对此的直接引用,但是您可以从包含对话框的对话框的场景中获取它:alert.initOwner(dialog.getDialogPane().getScene().getWindow());