Auto Populate DateField

Hi,

Working with an older version of Cuba (6.10) and have minimal experience with Cuba and Java.

I would like to auto populate a date field based on another date field. So that when someone enters a date in fieldA the entry for fieldA120 is populated with fieldA’s date + 120 days.

I modified this version - How to update fieldgroup property using a Datasource?

The app builds, but entering a date value in field A no other fields update.

I hope someone can point me in the right direction or another approach.

image

Here is my edit.xml

<window xmlns="http://schemas.haulmont.com/cuba/window.xsd"
        caption="msg://editorCaption"
        class="com.company.dateadditionexample.web.datetest.DateTestEdit"
        datasource="dateTestDs"
        focusComponent="fieldGroup"
        messagesPack="com.company.uhsdateadditionexample.web.datetest">
    <dsContext>
        <datasource id="dateTestDs"
                    class="com.company.uhsdateadditionexample.entity.DateTest"
                    view="_local"/>
    </dsContext>
    <dialogMode height="600"
                width="800"/>
    <layout expand="windowActions"
            spacing="true">
        <fieldGroup id="fieldGroup"
                    datasource="dateTestDs">
            <column width="250px">
                <field property="dateA"/>
                <field property="dateA120"/>
                <field property="dateb"/>
                <field property="dateb120"/>
            </column>
        </fieldGroup>
        <frame id="windowActions"
               screen="editWindowActions"/>
    </layout>
</window>

Here is my current Controller

import com.haulmont.cuba.gui.components.AbstractEditor;
import com.company.uhsdateadditionexample.entity.DateTest;
import com.haulmont.cuba.gui.components.DateField;

import  javax.inject.Named;
import java.util.Date;

public class DateTestEdit extends AbstractEditor<DateTest> {

@Named("fieldGroup.dateA")
private DateField dateAField;

@Named("fieldGroup.dateA120")
private DateField dateA120Field;

@Override
public void ready() {
    dateAField.addValueChangeListener(this::updatedateA120);
}

private void updatedateA120(ValueChangeEvent ignored) {
    if (dateAField.getValue() == null || dateA120Field.getValue() == null) {
        return;
    } 

   Date dateA = dateAField.getValue();
   Date dateA120 = dateA120Field.getValue();
   getItem().setDateA120(dateA);
  
   }

} 

Thank you for taking a look!

Hi @Stephen,
In your case it is better to use a DatasourceListener (ItemPropertyChangeListener) instead of a ValueChangeListener:

If the component is bound to a datasource, in terms of the screen lifecycle, it is often preferable to use a datasource listener instead.

There is an example in the docs:

@Override
public void init(Map<String, Object> params) {
	orderDs.addItemPropertyChangeListener(e -> {
		if ("customer".equals(e.getProperty())) {
			attributeAccessSupport.applyAttributeAccess(this, true, getItem());
		}
	});
}

Given your code, it should be something like this:

@Override
public void init(Map<String, Object> params) {
	dateTestDs.addItemPropertyChangeListener(e -> {
		if ("dateA".equals(e.getProperty())) {
			Calendar c = Calendar.getInstance();
			c.setTime(dateTestDs.getItem().getDateA());
			c.add(Calendar.DAY_OF_MONTH, 120);
			
			dateTestDs.getItem().setDateA120(c.getTime());
	});
}

P.S.: I don’t know if the code will compile, I just copied and pasted random codes from the internet and the cuba docs here, and I don’t use 6.10 for quite some time. But it should help you figure it out how to implement what you need.

Regards,
Peterson.

1 Like

Thank you - Working though your example. I appreciate the assistance.

Yup that was exactly it. Thank you!

1 Like