Announcement Announcement Module
Collapse
No announcement yet.
Field A + B = read-only field C Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Field A + B = read-only field C

    I have 2 editable fields (doubles) and a 3th read-only field that is the sum of the 2 other fields:

    Code:
            getFormModel().getFieldMetadata("c").setReadOnly(true);
    //...
            formBuilder.add("a");
            formBuilder.row();
            formBuilder.add("b");
            formBuilder.row();
            formBuilder.add("c");
            formBuilder.row();
    
    
        private class FinancingValueChangeMonitor extends AbstractValueChangeMonitor {
    //...
    
            public void onValueChange(Object oldValue, Object newValue) {
                Double a= (Double) getValueModel("a").getValue();
                Double b= (Double) getValueModel("b").getValue();
                Double c= a + b;
                getValueModel("c").setValue(c);
            }
        }
    This work well to edit the form, but when I try to save such a form I get the follow exception:
    org.springframework.beans.NotWritablePropertyExcep tion: Invalid property 'c' of bean class [x.X]

    X has a getter for a, b, c and a setter for a, b.
    Because c is calculated on a and b, it doesn't have a setter for that, which causes the array.
    How I can prevent form to setC on my X when a commit of the form occurs?

  • #2
    Currently, there is no builtin support for "synthized" fields. It's something that I've been wanting to add to Spring Rich for some time, but it has never made it to the top of my list.

    The simplest way to do what you want is to add the "c" field to the form as a JLabel instead of as a binding. Then, instead of calling getValueModel("c").setValue(c) you would call lblForMySynthField.setText( String.valueof(c) )

    That's how I've handled this situation in my apps.

    Larry.

    Comment


    • #3
      Thanks Larry (as always)
      I 'll try that.

      Comment


      • #4
        This way I am missing out on some features though:
        - i18n of the label (but I can do this with getComponentFactory().createLabel(...))
        - formatting of the double done correct, which is a lot harder.

        I am actually thinking of "hacking it" by creating a setter for the total that does nothing.

        Comment


        • #5
          Agreed, the JLabel solution isn't terrific, but works well for simple derived values.

          Larry.

          Comment


          • #6
            Hi all,

            I want to edit reportparameters on a form with changing meaning depending on the report. I can set the label-properties in my messages.xml. But these are fix settings and I need to change the labeltext. Is there a way to do this?

            regards
            divo

            Comment

            Working...
            X