public static Range showRangeDialog(String title, String header) { final Range[] retVal = {null}; Dialog<Range> dialog = new Dialog<>(); dialog.setTitle(title); dialog.setHeaderText(header); IntegerProperty fromFieldProperty = new SimpleIntegerProperty(), toFieldProperty = new SimpleIntegerProperty(); // dialog.setGraphic(new ImageView(this.getClass().getResource("login.png").toString())); // Set the button types. ButtonType generateButtonType = new ButtonType(ResourceManager.getMessage("label.button.ok"), ButtonBar.ButtonData.OK_DONE); dialog.getDialogPane().getButtonTypes().addAll(generateButtonType, ButtonType.CANCEL); // Create the username and password labels and fields. GridPane grid = new GridPane(); grid.setHgap(10); grid.setVgap(10); grid.setPadding(new Insets(20, 150, 10, 10)); TextField fromTextField = new TextField(); fromTextField.textProperty().bindBidirectional(fromFieldProperty, new NumberStringConverter()); TextField toTextField = new TextField(); toTextField.textProperty().bindBidirectional(toFieldProperty, new NumberStringConverter()); grid.add(new Label("From:"), 0, 0); grid.add(fromTextField, 1, 0); grid.add(new Label("To:"), 0, 1); grid.add(toTextField, 1, 1); // Enable/Disable login button depending on whether a username was entered. Node loginButton = dialog.getDialogPane().lookupButton(generateButtonType); loginButton.setDisable(true); // Do some validation (using the Java 8 lambda syntax). fromTextField .textProperty() .addListener( (observable, oldValue, newValue) -> { loginButton.setDisable( newValue.trim().isEmpty() || toTextField.getText().trim().isEmpty() || Integer.valueOf(newValue.trim()) .compareTo(Integer.valueOf(toTextField.getText().trim())) > 0); }); toTextField .textProperty() .addListener( (observable, oldValue, newValue) -> { loginButton.setDisable( fromTextField.getText().trim().isEmpty() || newValue.trim().isEmpty() || Integer.valueOf(fromTextField.getText().trim()) .compareTo(Integer.valueOf(newValue.trim())) > 0); }); dialog.getDialogPane().setContent(grid); // Request focus on the username field by default. Platform.runLater(() -> fromTextField.requestFocus()); // Convert the result to a username-password-pair when the login button is clicked. dialog.setResultConverter( dialogButton -> { if (dialogButton == generateButtonType) { return new IntegerRange(fromFieldProperty.get(), toFieldProperty.get()); } return null; }); Optional<Range> result = dialog.showAndWait(); result.ifPresent(range -> retVal[0] = range); return retVal[0]; }
public boolean showAndWait() { Optional<ButtonType> response = dialog.showAndWait(); response.filter(r -> r == ButtonType.CANCEL).ifPresent(r -> clearRecordingList()); response.filter(r -> r == ButtonType.OK).ifPresent(r -> clearCanceledRecordings()); return (response.isPresent() && response.get() == ButtonType.OK); }