Using Data Components

This section provides practical examples of working with data components.

Declarative Usage

Usually, data components are defined and bound to visual components declaratively in the screen XML descriptor. If you create a screen for an entity using Studio, you can see the top-level <data> element which contains the data component declarations.

Below is an example of data components in an edit screen for the Employee entity that has a to-one reference to Department and a to-many reference to the EquipmentLine entity:

<data> (1)
    <instance id="employeeDc"
              class="ui.ex1.entity.Employee"> (2)
        <fetchPlan extends="_base"> (3)
            <property name="department" fetchPlan="_instance_name"/>
            <property name="equipment" fetchPlan="_base"/>
        <collection id="equipmentDc" property="equipment"/> (5)
    <collection id="departmentsDc"
                fetchPlan="_base"> (6)
        <loader> (7)
                <![CDATA[select e from uiex1_Department e]]>
1 The root data element defines the DataContext instance.
2 InstanceContainer of the Employee entity.
3 The optional fetchPlan attribute defines the object graph that should be eagerly loaded from the database.
4 InstanceLoader that loads the Employee instance.
5 CollectionPropertyContainer for the nested EquipmentLine entity. It is bound to the collection attribute.
6 CollectionContainer for the Department entity.
7 CollectionLoader that loads the Department entity instances using the specified query.

The data containers defined above can be used in visual components as follows:

<layout spacing="true" expand="editActions">
    <textField dataContainer="employeeDc" property="id"/> (1)
    <form id="form" dataContainer="employeeDc"> (2)
        <column width="350px">
            <textField id="nameField" property="name"/>
            <textField id="salaryField" property="salary"/>
            <comboBox id="positionField" property="position"/>
            <entityComboBox property="department"
                            optionsContainer="departmentsDc"/> (3)
    <table dataContainer="equipmentDc"> (4)
            <column id="name"/>
            <column id="number"/>
1 Standalone fields have dataContainer and property attributes.
2 The form component propagates dataContainer to its fields, so they need only the property attribute.
3 The EntityComboBox field has also the optionsContainer attribute to get the list of options.
4 Tables have only the dataContainer attribute.

Programmatic Usage

Data components can be created and used in visual components programmatically.

In the example below, we create an editor screen with the same data and visual components as defined in the previous section using only Java code without any XML descriptor.

public class EmployeeEditExample extends StandardEditor<Employee> {

    private DataComponents dataComponents; (1)
    private UiComponents uiComponents;

    private InstanceContainer<Employee> employeeDc;
    private CollectionPropertyContainer<EquipmentLine> equipmentDc;
    private CollectionContainer<Department> departmentsDc;
    private InstanceLoader<Employee> employeeDl;
    private CollectionLoader<Department> departmentsDl;

    protected void onInit(InitEvent event) {

    private void createDataComponents() {
        DataContext dataContext = dataComponents.createDataContext();
        getScreenData().setDataContext(dataContext); (2)

        employeeDc = dataComponents.createInstanceContainer(Employee.class);

        employeeDl = dataComponents.createInstanceLoader();
        employeeDl.setContainer(employeeDc); (3)
        employeeDl.setDataContext(dataContext); (4)

        equipmentDc = dataComponents.createCollectionContainer(
                EquipmentLine.class, employeeDc, "equipment"); (5)

        departmentsDc = dataComponents.createCollectionContainer(Department.class);

        departmentsDl = dataComponents.createCollectionLoader();
        departmentsDl.setQuery("select e from uiex1_Department e"); (6)

    private void createUiComponents() {

        Form form = uiComponents.create(Form.class);

        TextField<String> nameField = uiComponents.create(TextField.TYPE_STRING);
        nameField.setValueSource(new ContainerValueSource<>(employeeDc, "name")); (7)

        TextField<Double> salaryField = uiComponents.create(TextField.TYPE_DOUBLE);
        salaryField.setValueSource(new ContainerValueSource<>(employeeDc, "salary"));

        ComboBox<Position> positionField = uiComponents.create(ComboBox.of(Position.class));
        positionField.setValueSource(new ContainerValueSource<>(employeeDc, "position"));

        EntityComboBox<Department> departmentField = uiComponents.create(EntityComboBox.of(Department.class));
        departmentField.setValueSource(new ContainerValueSource<>(employeeDc, "department"));
        departmentField.setOptions(new ContainerOptions<>(departmentsDc)); (8)

        Table<EquipmentLine> table = uiComponents.create(Table.of(EquipmentLine.class));
        table.setItems(new ContainerTableItems<>(equipmentDc)); (9)

        Button okButton = uiComponents.create(Button.class);
        okButton.addClickListener(clickEvent -> closeWithCommit());

        Button cancelButton = uiComponents.create(Button.class);
        cancelButton.addClickListener(clickEvent -> closeWithDiscard());

    protected InstanceContainer<Employee> getEditedEntityContainer() { (10)
        return employeeDc;

    protected void onBeforeShow(BeforeShowEvent event) { (11)
1 DataComponents is a factory to create data components.
2 The DataContext instance is registered in the screen for standard commit action to work properly.
3 The employeeDl loader will load data to employeeDc container.
4 The employeeDl loader will merge loaded entities into the data context for change tracking.
5 The equipmentDc is created as a property container.
6 A query is specified for the departmentsDl loader.
7 ContainerValueSource is used to bind single fields to containers.
8 ContainerOptions is used to provide options to combo boxes.
9 ContainerTableItems is used to bind tables to containers.
10 getEditedEntityContainer() is overridden to specify the container instead of @EditedEntityContainer annotation.
11 Loads data before showing the screen. The edited entity id will be set to employeeDl by the framework automatically.

Dependencies Between Data Components

Sometimes you need to load and display data that depends on other data in the same screen. For example, on the image below the left table displays the list of employees and the right one displays the list of equipment for the selected employee. The right list is refreshed each time the selected item in the left list changes.

depend tables

In this example, the Employee entity contains the equipment attribute that is a one-to-many collection. So the simplest way to implement the screen is to load the list of employees with a fetch plan containing the equipment attribute and use a property container to hold the list of dependent equiment lines. Then bind the left table to the master container and the right table to the property container.

But this approach has the following performance implication: you will load equipment lines for all employees from the left table, even though you display the equipment lines only for a single employee. This is why we recommend using property containers and deep fetch plans with collection attributes only when loading a single master item, for example, in the employee editor screen.

Also, the master entity may have no direct property pointing to the dependent entity. In this case, the above approach with a property container would not work at all.

The common approach to organize relations between data in a screen is to use queries with parameters. The dependent loader contains a query with a parameter that links data to the master, and when the current item in the master container changes, you set the parameter and trigger the dependent loader.

Below is an example of the screen which has two dependent container/loader pairs and the tables bound to them.

<window xmlns=""
        <collection id="employeesDc"
                    fetchPlan="_base"> (1)
            <loader id="employeesDl">
                    <![CDATA[select e from uiex1_Employee e]]>
        <collection id="equipmentLinesDc"
                    fetchPlan="_base"> (2)
            <loader id="equipmentLinesDl">
                    <![CDATA[select e from uiex1_EquipmentLine e where e.employee = :employee]]>
    <facets> (3)
        <screenSettings id="settingsFacet" auto="true"/>
        <hbox id="mainBox" width="100%" height="100%" spacing="true">
            <table id="employeesTable" width="100%" height="100%"
                   dataContainer="employeesDc"> (4)
                    <column id="name"/>
                    <column id="salary"/>
                    <column id="position"/>
            <table id="equipmentLinesTable" width="100%" height="100%"
                   dataContainer="equipmentLinesDc"> (5)
                    <column id="name"/>
                    <column id="number"/>
1 Master container and loader.
2 Dependent container and loader.
3 The DataLoadCoordinator facet is not used, so the loaders will not be triggered automatically.
4 Master table.
5 Dependent table.
public class EmployeeDependTables extends StandardLookup<Employee> {

    private CollectionLoader<Employee> employeesDl;

    private CollectionLoader<EquipmentLine> equipmentLinesDl;

    public void onBeforeShow(BeforeShowEvent event) {
        employeesDl.load(); (1)

    @Subscribe(id = "employeesDc", target = Target.DATA_CONTAINER)
    public void onEmployeesDcItemChange(InstanceContainer.ItemChangeEvent<Employee> event) {
        equipmentLinesDl.setParameter("employee", event.getItem()); (2)
1 The master loader is triggered in the BeforeShowEvent handler.
2 In the ItemChangeEvent handler of the master container, a parameter is set to the dependent loader and it is triggered.
The DataLoadCoordinator facet allows you to link data components declaratively without writing any Java code.

Using Screen Parameters in Loaders

It is often required to load data in a screen depending on parameters passed to that screen. Below is an example of a browse screen that accepts a parameter and uses it to filter the loaded data.

Suppose we have two entities: Country and City. The City entity has the country attribute that is a reference to Country. The cities browser accepts a country instance and shows cities only of this country.

First, consider the cities screen XML descriptor. Its loader contains a query with a parameter:

    <collection id="citiesDc"
        <loader id="citiesDl">
                <![CDATA[select e from uiex1_City e where = :country]]>

The cities screen controller contains a public setter for the parameter and uses this parameter in the BeforeShowEvent handler.

public class CityBrowse extends StandardLookup<City> {

    private CollectionLoader<City> citiesDl;

    private Country country;

    public void setCountry(Country country) { = country;

    public void onBeforeShow(BeforeShowEvent event) {
        if (country == null)
            throw new IllegalStateException("Country parameter is null");
        citiesDl.setParameter("country", country);

The cities screen can be opened from another screen passing a country as follows:

private ScreenBuilders screenBuilders;

private void showCitiesOfCountry(Country country) {
    CityBrowse cityBrowse = screenBuilders.screen(this)

Custom Sorting

Sorting of UI tables by entity attributes is performed by CollectionContainerSorter which is set for a CollectionContainer. The standard implementation sorts data in memory if it fits in one page of loaded data, otherwise it sends a new request to the database with the appropriate "order by" clause. The "order by" clause is created by the JpqlSortExpressionProvider bean.

Some entity attributes can require a special implementation of sorting. Below we explain how to customize sorting on a simple example: suppose there is the Order entity with a number attribute of type String, but we know that the attribute actually stores only numeric values. So we want the sort order to be 1, 2, 3, 10, 11. With the default behavior, the order would be 1, 10, 11, 2, 3.

First, create a subclass of the CollectionContainerSorter class for sorting in memory:

public class CustomCollectionContainerSorter extends CollectionContainerSorter {

    public CustomCollectionContainerSorter(CollectionContainer container,
                                           @Nullable BaseCollectionLoader loader,
                                           BeanFactory beanFactory) {
        super(container, loader, beanFactory);

    protected Comparator<?> createComparator(Sort sort, MetaClass metaClass) {
        MetaPropertyPath metaPropertyPath = Objects.requireNonNull(

        if (metaPropertyPath.getMetaClass().getJavaClass().equals(Order.class)
                && "num".equals(metaPropertyPath.toPathString())) {
            boolean isAsc = sort.getOrders().get(0).getDirection() == Sort.Direction.ASC;
            return Comparator.comparing((Order e) ->
                            e.getNum() == null ? null : Integer.valueOf(e.getNum()),
                    new EntityValuesComparator<>(isAsc, metaClass, beanFactory));
        return super.createComparator(sort, metaClass);

Create the sorter in the required screen:

public class OrderBrowseExample extends StandardLookup<Order> {

    private CollectionLoader<Order> ordersDl;

    private CollectionContainer<Order> ordersDc;

    private BeanFactory beanFactory;

    private void onInit(InitEvent event) {
        Sorter sorter = new CustomCollectionContainerSorter(ordersDc, ordersDl, beanFactory);

If your sorter defines some global behavior, create your own factory that instantiates sorters system-wide:

public class CustomSorterFactory extends SorterFactory {

    public Sorter createCollectionContainerSorter(CollectionContainer container,
                                                  @Nullable BaseCollectionLoader loader) {
        return new CustomCollectionContainerSorter(container, loader, beanFactory);

Also, you can create own implementation of JpqlSortExpressionProvider for sorting at the database level:

public class CustomSortExpressionProvider
        extends DefaultJpqlSortExpressionProvider {

    public String getDatatypeSortExpression(MetaPropertyPath metaPropertyPath, boolean sortDirectionAsc) {
        if (metaPropertyPath.getMetaClass().getJavaClass().equals(Order.class)
                && "num".equals(metaPropertyPath.toPathString())) {
            return String.format("CAST({E}.%s BIGINT)", metaPropertyPath);
        return String.format("{E}.%s", metaPropertyPath);