CRUD Form Attempt

Hi Guys

I am a newbie to the forums and I was hoping somebody might be able to explain to me where I’m going wrong. I am playing around with a CRUD form design based on a couple of tutorials I have read to try and understand the field group binding better. I seem to be able to generate a form ok however I have one peculiar hickup that I don’t know how to fix. When I use the build and bind function from the fieldgroup class only the int’s are able to be modified, all string values are read only. Can someone help? My code is as follows:

public class CrudWindow extends Window {
private final Person p = new Person();
private final FieldGroup fieldGroup;
private final FormLayout layout = new FormLayout();

CrudWindow() {
    this.setWidth("600px");
    this.setHeight("500px");
    this.setCaption("CRUD Window");
    this.center();
    fieldGroup = new BeanFieldGroup<Person>(Person.class);
    fieldGroup.setItemDataSource(new BeanItem<Person>(p));
    
    for(Object propertyId : fieldGroup.getUnboundPropertyIds()) {
        layout.addComponent(fieldGroup.buildAndBind(propertyId));
    }
    this.setContent(layout);
}

}

I have attached the screenshot of what this looks like.

Thank you!!
15232.png

Does your Person class have properly named setter methods? The BeanItem uses those to determine ReadOnly status.

Hi Thomas

Thanks for your reply :slight_smile:

I have getters and setters (I just autogenerated them through the netbeans function).

Sorry for the silly question but what is the difference between a standard setter method and a named one?

For example my class looks something like this (I’m at work so I’m just making a class up for the example :)):

public class person {
private int id;
private string FirstName;
private string LastName;
// more stuff here

//setters
public void setFName(String FN) {
FirstName = FN;
}
public void setLName(String LN) {
FirstName = LN;
}
// getters here
// more functional goodness here
}

Is a named setter different to this?

Thanks in advance

Mike

The standard java naming conventions specify that setters and getters need to have the same name as the field, so in your case:

private String firstName;

public void setFirstName(String whatever){}
public String getFirstName(){};

Please change you bean class and try it again.

Hi Again

Here is the exact Person Class:
public class Person {
private int id;
@Size(min=3, max=30)
private String FirstName = “”;
private String LastName = “”;
private String AddressName = “”;
private String SuburbName = “”;
private String StateName = “”;
private String Postcode = “”;
private int number;
private Date CreateDate;

public Person(int id, String f, String l, String a,String s, String st,String p) {
    this.setId(id);
    this.setFirstName(f);
    this.setLastName(l);
    this.setAddressName(a);
    this.setStateName(st);
    this.setSuburbName(s);
    this.setPostcode(p);
}

public Person() {
    //this.setId(0);
    this.setFirstName("");
    this.setLastName("");
    this.setAddressName("");
    this.setStateName("");
    this.setSuburbName("");
    this.setPostcode("");
    
}
/**
 * @return the FirstName
 */
public String getFirstName() {
    return FirstName;
}

/**
 * @param FirstName the FirstName to set
 */
private void setFirstName(String FirstName) {
    this.FirstName = FirstName;
}

/**
 * @return the LastName
 */
public String getLastName() {
    return LastName;
}

/**
 * @param LastName the LastName to set
 */
private void setLastName(String LastName) {
    this.LastName = LastName;
}

/**
 * @return the AddressName
 */
public String getAddressName() {
    return AddressName;
}

/**
 * @param AddressName the AddressName to set
 */
private void setAddressName(String AddressName) {
    this.AddressName = AddressName;
}

/**
 * @return the SuburbName
 */
public String getSuburbName() {
    return SuburbName;
}

/**
 * @param SuburbName the SuburbName to set
 */
private void setSuburbName(String SuburbName) {
    this.SuburbName = SuburbName;
}

/**
 * @return the StateName
 */
public String getStateName() {
    return StateName;
}

/**
 * @param StateName the StateName to set
 */
private void setStateName(String StateName) {
    this.StateName = StateName;
}

/**
 * @return the Postcode
 */
public String getPostcode() {
    return Postcode;
}

/**
 * @param Postcode the Postcode to set
 */
private void setPostcode(String Postcode) {
    this.Postcode = Postcode;
}

/**
 * @return the id
 */
public int getId() {
    return id;
}

/**
 * @param id the id to set
 */
public final void setId(int id) {
    this.id = id;
}

/**
 * @return the number
 */
public int getNumber() {
    return number;
}

/**
 * @param number the number to set
 */
public void setNumber(int number) {
    this.number = number;
}

/**
 * @return the CreateDate
 */
public Date getCreateDate() {
    return CreateDate;
}

/**
 * @param CreateDate the CreateDate to set
 */
public void setCreateDate(Date CreateDate) {
    this.CreateDate = CreateDate;
}

}

This looks like what you suggested before, is there something that I have missed?

the field names should start with a non-capital letter, otherwise you should be good to go. does it work?

I changed one of the getter and setter methods too this:

public String getfirstName() {
    return FirstName;
}

private void setfirstName(String FirstName) {
    this.FirstName = FirstName;
}

No good :frowning: The field is still set to read only.

Any other suggestions?

I’ve not been following this, but for javabeans you should use the common pattern:

assume String property: firstName

public String getFirstName() { return firstName; }
public void setFirstName(String v) { firstName = v; }

Using a private setter seems to indicate you don’t want it to be a javabean property that can be updated (hence readonly), and I’m not sure about the case strategy you are using as the one I show is most typical.

David you’re a genious, it worked!!!

to be honest I let netbeans add the getters and setters for me, I didn’t even notice that :slight_smile:

Thank you so much!!