TheTriggered27
TheTriggered27

Reputation: 21

JavaFX - How to check if a Datepicker has valid input

I'm making a to-do list in JavaFX. I want people to choose a deadline, so I'm using a Datepicker. However, I can't find out how to check if a person has input a valid Date. Can anybody help me with this?

If it is invalid, I want the system to alert the user with invalid input. Say I don't enter a label and/or datepicker, Java will alert the user with this invalid input by making use of a StringBuilder

With invalid input, I mean "Not a Date" and/or no input at all

Edit: I found it! The datepicker has a .getValue() method I can check if it has a null value.

Upvotes: 0

Views: 830

Answers (1)

VGR
VGR

Reputation: 44338

You can get the DatePicker’s text directly from its editor, and pass it to the DatePicker’s converter:

try {
    datePicker.getConverter().fromString(
        datePicker.getEditor().getText());
} catch (DateTimeParseException e) {
    // Alert user here
}

Upvotes: 1

Related Questions