Select or ComboBox does not Show Selected Property - Solved

Hi all…

I’m building a Form that haves binds a POJO (Connection), and that POJO has references to another POJO(Database), like that:


class Connection {
 String connectionName;
 String username;
 String password;
 Database database;

}

class Database {
 String databaseName;
 String jdbcDriver;
}

I alread have a separated form, that managed the Database entity, and it is working nice. In the ‘Connection’ form, I have a Select component that shows the Databases avaliable, and it works too. If Im creating a new Entity, I piked a value from the select box, and the value is persisted fine.
But, if I want to modify a Entity, the database that is associated with the ‘Connection’ entity is not selected as default on the Select, or ComoBox as I tested.

Other fields, that uses TextField haves their values seted OK.

I try many things, like force the selection before the form is created, with


form.getItemProperty("database").setValue("databaseFromItem")

Im creating the field, with a custom FormFieldFactory:


private class ConexaoFieldFactory extends DefaultFieldFactory {
        
        @Override
        public Field createField(Item item, Object propertyId, Component uiContext) {
            String pid = (String) propertyId;
            if(pid.equals("sigla")) {
                TextField t = new TextField("Sigla");
                t.setRequired(true);
                t.setRequiredError("Por favor, informe uma sigla");
                t.addValidator(new StringLengthValidator("A sigla deve conter entre 3 e 20 caracteres", 3, 20, false));
                t.setNullRepresentation("");
                return t;
            }
            else if(pid.equals("databaseHost")) {
                TextField t = new TextField("Host");
                t.setRequired(true);
                t.setColumns(20);
                t.setRequiredError("Por favor, informe a classe HOST para este banco de dados");
                t.setNullRepresentation("");
                return t;
            }
            else if(pid.equals("database")) {
                POJOContainer<Database> container;
                DatabasesController controller = new DatabasesController();
                List<Database> dbs = controller.getAllDatabases();
                if(dbs.isEmpty()) {
                    container = new POJOContainer<Database>(Database.class, "sigla");
                }
                else {
                    container = new POJOContainer<Database>(dbs, "sigla");
                }
                Select t = new Select("Databases", container);
                t.setItemCaptionMode(AbstractSelect.ITEM_CAPTION_MODE_PROPERTY);
                t.setItemCaptionPropertyId("sigla");
                t.setRequired(true);
                t.setNullSelectionAllowed(false);
                t.setMultiSelect(false);
                return t;
            }
            return null;
        }
    }

My form is a custom form too, extended from the Form class, to override attachField, and position the fields freely.

Any help is apreciated. Ive read some topics on this forum, but nothing that I found solve my problem…


Eduardo Frazão

Probably you have to add the line t.setImmediate(true);

Unfortunately, the setImmediate does not works.

When I get the property Value of the form, the value was correctly set, but the Select does not shows it… :frowning:

Hi all…
Ive found another topic on that forum, that shows the path :)…

Ive changed my Entity Superclass to suporte gereration of hashCode, and then a decent equals method, and on entityes, I generate the hashCode and equals with eclipse helper.

My hashCode and equals method becomes like that:


@Override
    public int hashCode() {
        final int prime = 31;
        int result = super.hashCode();
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!super.equals(obj)) {
            return false;
        }
        if (!(obj instanceof Database)) {
            return false;
        }
        Database other = (Database) obj;
        if (id == null) {
            if (other.id != null) {
                return false;
            }
        } else if (!id.equals(other.id)) {
            return false;
        }
        return true;
    }

Many thanks. Not its working.