binding
  1. binding
  2. BINDING-139

FormattedTextfield bound to java.lang.Double results in exception when user is not typing in a decimals separator

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Blocker Blocker
    • Resolution: Unresolved
    • Affects Version/s: 2.2.0
    • Fix Version/s: None
    • Component/s: core
    • Labels:
      None
    • Environment:

      Actual Version is 2.2.1, but "Affects Version/s" list box only contains 2.2.0 and 2.2.2

      Description

      We have bound a java.lang.Double property to a formatted text field using code similar to this:

      class MyBean {
      private Double x;
      public Double getX()

      {return this.x;}

      public void setX(Double x)

      {this.x = x;}

      }

      MyBean b = new MyBean();

      BeanAdapter<MyBean> a = new BeanAdapter<MyBean>(b);

      JComponent c = BasicComponentFactory.createFormattedTextField(a.getValueModel("x"), NumberFormat.getInstance());

      Problem Description:

      User types in a floating point number that could be interpreted as an integer (like "45,000" or just "45") --> results in exception thrown by JGoodies:

      Failed to set an adapted Java Bean property.
      cause=java.lang.IllegalArgumentException: argument type mismatch
      bean=MyBean@27af8502
      bean type=MyBean
      value=44
      value type=java.lang.Long <--- This is wrong, as "45,000" is a java.lang.Double!
      property name=nominalValue
      property type=java.lang.Double
      property setter=public final void MyBean.setX(java.lang.Double)

      Severity:

      This is a showstopper for us. We do not see any workaround besides faking the typed in number to be "45,001" instead of "45,000" which would work but obviously is inacceptable from the view of the user.

        Activity

        Hide
        karsten added a comment -

        First off, to understand this issue, please provide the Locale you are using; in examples create Formats or Formatters with a specific Locale, e.g. NumberFormat.getInstance(Locale.ENGLISH).

        "45,000" is neither a Long nor a Double. It is a string that you can parse to convert it into a number. The parse result of the NumberFormat you use is a Java Long. java.text.NumberFormat.getInstance(java.util.Locale.ENGLISH).parse("45,000") -> Long(45000)

        You should provide a Format or Formatter that parses the number string the way you want. The JGoodies Binding works as expected.

        I think Hans Muller has an old blog entry that describes how to setup a useful decimal format. In this blog entry he mentiones how much work is required to get such standard things to work.

        Show
        karsten added a comment - First off, to understand this issue, please provide the Locale you are using; in examples create Formats or Formatters with a specific Locale, e.g. NumberFormat.getInstance(Locale.ENGLISH). "45,000" is neither a Long nor a Double. It is a string that you can parse to convert it into a number. The parse result of the NumberFormat you use is a Java Long. java.text.NumberFormat.getInstance(java.util.Locale.ENGLISH).parse("45,000") -> Long(45000) You should provide a Format or Formatter that parses the number string the way you want. The JGoodies Binding works as expected. I think Hans Muller has an old blog entry that describes how to setup a useful decimal format. In this blog entry he mentiones how much work is required to get such standard things to work.
        Hide
        mkarg added a comment -

        Karsten,

        thank you for your kind answer.

        The used locale actually is Locale.GERMAN.

        I checked Sun's sources of NumberFormat and DecimalFormat and the JavaDocs clearly say that whenever possible it will return a Long and if a Double is needed, the caller (i. e. JGoodies in this case) must request it using doubleValue(). So since that is documented (and wanted) behaviour of the JRE, JGoodies' factory method used to bind should obiously be able to deal with that.

        Otherwise it would be kind of you if you could post how to configure JGoodies Binding to return bindings that will work with Double properties. If it's about paid support, just let me know.

        Unfortunately I couldn't find Hans Muller's blog entry which you mentioned and do not see how it can work without fixing it inside of JGoodies.

        Thanks
        Markus

        Show
        mkarg added a comment - Karsten, thank you for your kind answer. The used locale actually is Locale.GERMAN. I checked Sun's sources of NumberFormat and DecimalFormat and the JavaDocs clearly say that whenever possible it will return a Long and if a Double is needed, the caller (i. e. JGoodies in this case) must request it using doubleValue(). So since that is documented (and wanted) behaviour of the JRE, JGoodies' factory method used to bind should obiously be able to deal with that. Otherwise it would be kind of you if you could post how to configure JGoodies Binding to return bindings that will work with Double properties. If it's about paid support, just let me know. Unfortunately I couldn't find Hans Muller's blog entry which you mentioned and do not see how it can work without fixing it inside of JGoodies. Thanks Markus
        Hide
        mkarg added a comment -

        Having written the above comment I had the idea of coding a decorating Format class always calling doubleValue() on a provided NumberFormat instance. In a short test (without JGoodies) it worked well. Will try it on Monday with JGoodies.

        Nevertheless it would be great if JGoodies would solve it "under the hood", as it knows it needs a Double to sync with the bound property. As the binding is done by JGoodies' factory method, this would be much more convenient than having to decorate manually. I mean, it is not a seldomcase to bind to Double.

        Show
        mkarg added a comment - Having written the above comment I had the idea of coding a decorating Format class always calling doubleValue() on a provided NumberFormat instance. In a short test (without JGoodies) it worked well. Will try it on Monday with JGoodies. Nevertheless it would be great if JGoodies would solve it "under the hood", as it knows it needs a Double to sync with the bound property. As the binding is done by JGoodies' factory method, this would be much more convenient than having to decorate manually. I mean, it is not a seldomcase to bind to Double.

          People

          • Assignee:
            karsten
            Reporter:
            mkarg
          • Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated: