Results 1 to 6 of 6

Thread: Field A + B = read-only field C

  1. #1
    Join Date
    May 2005
    Posts
    394

    Default 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. #2
    Join Date
    Aug 2005
    Location
    Austin, TX
    Posts
    425

    Default

    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.

  3. #3
    Join Date
    May 2005
    Posts
    394

    Default

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

  4. #4
    Join Date
    May 2005
    Posts
    394

    Default

    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.

  5. #5
    Join Date
    Aug 2005
    Location
    Austin, TX
    Posts
    425

    Default

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

    Larry.

  6. #6
    Join Date
    Jun 2006
    Posts
    8

    Default

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •