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

DatePicker updates its value property with wrong date when dialog closes

XMLWordPrintable

    • b10
    • b12
    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      When the user changes the value of a DatePicker interactively via the dropdown and the value change at the same time triggers the closing of the dialog where the picker is being displayed then the new code for updating the date value upon "focus lost" will use the old date that is still being displayed inside the editor field (the textfield).

      REGRESSION : Last worked in version openjfx17

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Expecting the newly selected date and ONLY the newly selected date.
      ACTUAL -
      First we receive the new date, then the code for handling "focus lost" will revert the date based on the text found inside the editor field.

      ---------- BEGIN SOURCE ----------
      // Provided by Dirk Lemmermann
      import javafx.application.Application;
      import javafx.scene.Scene;
      import javafx.scene.control.Alert;
      import javafx.scene.control.Alert.AlertType;
      import javafx.scene.control.Button;
      import javafx.scene.control.DatePicker;
      import javafx.stage.Stage;

      import java.time.LocalDate;

      public class DatePickerBug extends Application {
          
          @Override
          public void start(Stage primaryStage) throws Exception {
              DatePicker picker = new DatePicker(LocalDate.now());

              Alert dialog = new Alert(AlertType.INFORMATION);
              dialog.setTitle("Date Picker");
              dialog.getDialogPane().setContent(picker);

              Button button = new Button("Show Dialog");
              button.setOnAction(evt -> {
                  dialog.show();
              });

              picker.valueProperty().addListener(it -> {
                  System.out.println("date: " + picker.getValue());
                  dialog.close();
              });

              Scene scene = new Scene(button);
              primaryStage.setTitle("Date Picker Bug");
              primaryStage.setScene(scene);
              primaryStage.setWidth(300);
              primaryStage.setHeight(250);
              primaryStage.centerOnScreen();
              primaryStage.show();
          }

          public static void main(String[] args) {
              launch(args);
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Manually update the editor field before dealing with the new value. Example:

              datePicker.valueProperty().addListener((obs, oldValue, newValue) -> {
                      datePicker.getEditor().setText(datePicker.getConverter().toString(newValue));
                      // do your thing
                     .....
              });


      FREQUENCY : always


            kpk Karthik P K
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: