NativeSelect is shown as not enabled/read only

Hello all,

I have a simple form where three fields are shown, name, address, active. The active field is of type NativeSelect and the corresponding variable in the domain object is of type Boolean. In the domain object the corresponding getter and setter is responsible for the component’s values.

The problem is that the NativeSelect component is shown as not enabled and read only when the form is first created. Why?

I had a feeling that something goes wrong with the toString() method for the active field in the domain object (therefore the getActive() method returns “” value in case active is null) but apart from this I can’t think anything else as the source of the problem.

Any help would be appreciated :slight_smile:

Cheers,
Natasa

Here is the form that contains the NativeSelect component


public class OrganizationForm extends Form implements ClickListener {
public OrganizationForm() {
                setWriteThrough(false);
                this.setImmediate(true);
		
		setFormFieldFactory(new DefaultFieldFactory() {
			@Override
			public Field createField(Item item, Object propertyId, Component uiContext) {
				if (propertyId.equals("name")) {
					TextField name = new TextField("Name");
					name.setNullRepresentation("");
					return name;
				}
				if (propertyId.equals("address")) {
					TextField address = new TextField("Address");
					address.setNullRepresentation("");
					return address;
				}
				if (propertyId.equals("active")) {
					String[] activeNotActiveValues = {"Active", "Not active"};
					active = new NativeSelect("Active", Arrays.asList(activeNotActiveValues));
					active.setNullSelectionAllowed(true);
					active.setReadOnly(false);
					active.setEnabled(true);
					active.setMultiSelect(false);
					active.setNewItemsAllowed(false);
					return active;
				}
				return super.createField(item, propertyId, uiContext);
			}
		});

		HorizontalLayout footer = new HorizontalLayout();
                footer.setSpacing(true);
               setFooter(footer);
	}
}

The corresponding domain object is Organization


public class GdOrganization implements Serializable {
	
	@Id
	@GeneratedValue
	private Long id;
	
	private String name;
	
	private String address;
	
	@Temporal(TemporalType.TIMESTAMP)
	private Date creationDate;
	
	private Boolean active;

	public String getActive() {
		if (active != null) {
			if (active) {
				return "Active";
			}
			else {
				return "Not active";
			}
		}
		return "";
	}

	public void setActive(Boolean input) {
		if (input != null) {
			if (input.equals("Active")) {
				active = true;
			}
			else {
				active = false;
			}
		}
		else {
			active = null;
		}
	}

Hi Natasa,

the getter and setter for active don’t look ok. The get-method returns a string and the set-method expects Boolean. Change the attribute and methods to String an it should work.

Greatings,
Pascal

You’re totally right! :*) I couldn’t find the error although it was so trivial!

thank you so much Pascal!

Natasa