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

Spinner's converter should update its editor

    XMLWordPrintable

Details

    • b10
    • x86_64
    • linux

    Description

      A DESCRIPTION OF THE REQUEST :
      According to the Spinner documentation (http://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Spinner.html), “The Spinner editor stays in sync with the value factory by listening for changes to the value property of the value factory.”.

      However, the Spinner’s value factory also has a converter, which is responsible for converting values from and to Strings that can be displayed (and potentially changed) in the editor. According to the specification, the editor does not listen to this converter property, which is inconvenient when the converter changes.

      JUSTIFICATION :
      A spinner’s converter indicates the format of the expected String representation of values inside the editor; not only will the user see values in that format, but they will be expected to type values in that format as well, if the Spinner is editable.

      As of now, the editor does not immediately reflect the new format when the converter changes. This behavior can lead the user to type values in a format supported by a previous converter, but not necessarily in the same way (or at all) by the newly set converter, which is confusing.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Given spinner a Spinner and value its current value,

      when spinner.getValueFactory().converterProperty() is updated from oldConverter to newConverter,

      then spinner.getEditor().getText().equals(newConverter.toString(value)).
      ACTUAL -
      Given spinner a Spinner and value its current value,

      when spinner.getValueFactory().converterProperty() is updated from oldConverter to newConverter,

      then spinner.getEditor().getText().equals(oldConverter.toString(value)).

      ---------- BEGIN SOURCE ----------
      import java.text.DecimalFormat;
      import java.text.ParseException;

      import javafx.application.Application;
      import javafx.scene.Scene;
      import javafx.scene.control.Spinner;
      import javafx.stage.Stage;
      import javafx.util.StringConverter;

      public class SpinnerRFE extends Application {
      @Override public void start(Stage primaryStage) throws Exception {
      Spinner<Double> spinner = new Spinner<>(0., 1., .5, .01);
      spinner.setEditable(true);
      spinner.getValueFactory().setConverter(new StringConverter<Double>() {
      private final DecimalFormat df = new DecimalFormat("#.##%");
      @Override public String toString(Double value) {
      return value == null ? "" : df.format(value);
      }
      @Override public Double fromString(String value) {
      if (value == null) {
      return null;
      }
      try {
      return df.parse(value).doubleValue();
      } catch (ParseException e) {
      throw new RuntimeException(e);
      }
      }
      });
      Scene scene = new Scene(spinner);
      primaryStage.setScene(scene);
      primaryStage.show();
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      It is possible to manually set the editor’s text property:
      spinner.getEditor().setText(spinner.getValueFactory().getConverter().toString(spinner.getValueFactory().getValue()));

      Another workaround is to update the spinner’s value property, however simply setting the spinner’s current value doesn’t fire an event. With this method, the only way to be absolutely sure that the editor gets updated would be to set the spinner’s value to two distinct values of T and then back to the original value.

      Attachments

        Issue Links

          Activity

            People

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

              Dates

                Created:
                Updated:
                Resolved: