Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8254368

DatePicker - Not Committing Value on Focus Loss

XMLWordPrintable

    • x86_64
    • windows_10

      ADDITIONAL SYSTEM INFORMATION :
      JavaFX 15 / Windows 10 / Oracle OpenJDK 15

      A DESCRIPTION OF THE PROBLEM :
      The DatePicker control does not commit/update its value on focus loss.

      This bug is related to various other JBS issues but some issues say they are fixed, others do not, some say regression, etc. - it's confusing and I believe the bug may have gotten lost in the shuffle. I apologize ahead of time if this is duplication (feel free to mark it as so or delete it), but I am unable to comment/inquire on the other issues.

      https://bugs.openjdk.java.net/browse/JDK-8191995
      https://bugs.openjdk.java.net/browse/JDK-8136838

      REGRESSION : Last worked in version 8u261

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Type in random alpha characters into the DatePicker Editor TextBox.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The text of the DatePicker Editor is returned to its previously valid value.
      ACTUAL -
      The random text remains.

      CUSTOMER SUBMITTED WORKAROUND :
      DatePicker datePicker = new DatePicker();
      datePicker.setConverter(new StringConverter<>(){
          @Override
          public String toString(LocalDate value){
              return value != null ? DateTimeFormatter.ofPattern("M/d/yyyy").format(value) : "";
          }

          @Override
          public LocalDate fromString(String value){
              if(value != null && !value.isBlank()){
                  try{
                      String pattern = value.matches("[0-9]{1,2}/[0-9]{1,2}/[0-9]{2}") ? "M/d/yy" : "M/d/yyyy";
                      return LocalDate.parse(value, DateTimeFormatter.ofPattern(pattern));
                  }
                  catch(DateTimeParseException e){
                      return null;
                  }
              }

              return null;
          }
      });
      datePicker.focusedProperty().addListener((observable, oldValue, newValue) -> {
          if(!newValue /** Focus Loss **/ && datePicker.isEditable()){
              LocalDate newDateValue = datePicker.getConverter().fromString(datePicker.getEditor().getText());
              if(newDateValue != null && !newDateValue.equals(datePicker.getValue())) // Set DatePicker Value
                  datePicker.setValue(newDateValue);
              else // Revert Editor Text
                  datePicker.getEditor().setText(datePicker.getConverter().toString(datePicker.getValue()));
          }
      });

      FREQUENCY : always


            pnarayanaswa Praveen Narayanaswamy
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: