I currently have a dialog with four TextFields from which the input is collected. When the input can not meet my requirements, the dialog should stay open and show an error message. I currently have the following code:
dlg.setResultConverter(dialogButton -> {
if (dialogButton == createButtonType) {
ArrayList<String> returnList = getInput();
if (returnList != null) {
return returnList;
}
else {
dlg.showAndWait();
}
}
return null;
});
Optional<ArrayList<String>> result = dlg.showAndWait();
result.ifPresent(customerDetails -> {
//Use the input for something
});
The getInput()
method gets the text from the TextFields and tests it against some requirements. When the input passes, the four Strings are put in an ArrayList<String>
. Else, the method returns null
. As you can see, the result converter checks if the method returned null
. If it didn't, it just returns the ArrayList<String>
.
But, when the validation fails, the dialog should be kept open. It works with dlg.showAndWait()
, but the downside is that it triggers an error: java.lang.IllegalStateException: Stage already visible
. (it keeps working, but getting an error is not the way it should be)
My question is: does anyone know how to do this, without triggering an error?
Note: I am using JavaFX 8 (JDK 1.8.0_40), which has the dialog features built in.