how does it work? And why doesn't it work for me?

So, I have a FieldFactory that implements the method createField(Item item, Object propertyId, Component uiContext) (and all the other methods as well for that matter although it turns out they’re not needed :slight_smile:

Anyway, I want to return a Select with a preset value. But I can’t seem to preset anything. I have BeanArrayContainer (found in the incubator at the time or writing) and I give it an array of pojos. I then try to select one of the pojos with no success.

I’ve even gone as far as to loop through all the itemIds in the container, print them out and selected one of them successfully but it just doesn’t show up as selected in the Select dropdown.

This is what the code looks like right now with all the looping and stuff but I hope there is some easier way I just haven’t found out about…

Where should I look? The container? My pojos? Any hints?

public Field createField(Item item, Object propertyId, Component uiContext) {
        Property property = item.getItemProperty(propertyId);

        if (property.getType().equals(XsltFile.class)) {
            XsltFile file = (XsltFile) property.getValue();
            ArrayList<XsltFile> listOfStylesheets = new ArrayList<XsltFile>(

            BeanArrayContainer ic = new BeanArrayContainer(listOfStylesheets);
            Select dropdown = new Select("Tyylitiedosto : ", ic);
            for (Object o : ic.getItemIds()) {
                System.out.println("::" + o);
                XsltFile file2 = (XsltFile) o;
                if (file.getId() == file2.getId()) {

            return dropdown;

I guess your problem is that you are selecting a value in a function meant for creating a field. If you for instance use your field factory in a form, the form will set a data source for the field automatically after it has been created. The value of the select will then be taken from the value of the property, effectively overwriting your value.

Yes, mr Anonymous got it right.

If you want to have default values selected, you need to either set them in the constructor of your POJO or in your POJO factory or somesuch…

The default value needs to be set in the POJO before the field factory is invoked (the form / table / whatever is built).


Expanding on previous posters explanation:

The Forms reflects the value of the Item that it is bound to, i.e the Field gets the value of the Item Property that is bound to the Bean (pojo).

You if you manage to set the value after the Field has been created and initialized by the Form, it will ‘stick’ ( f.getField(“foo”).setValue(“Muh”) below). Note that you must do this after each time the Form creates the Fields.

More complete example:

    public void init() {

        final Window main = new Window();

        Form f = new Form();
        f.setFieldFactory(new BaseFieldFactory() {
            public Field createField(Item item, Object propertyId,
                    Component uiContext) {
                ComboBox foos = new ComboBox("Foo");

                // Form overrides this with "Bar" from the pojo
                return foos;
        f.setItemDataSource(new BeanItem(new MyPojo()));

        // This will actually stick, since it happens after the Form stuff

    public class MyPojo {
        String foo = "Bar";

        public String getFoo() {
            return foo;

        public void setFoo(String foo) {
   = foo;


Thanks guys, the example was awesome! It makes sense not to set a value when defining the field - but my pojo still doesn’t give it a value. Below is my hibernate entity bean that is used as a pojo - and as you can see from my previous code snippet i use

[/quote] although I guess that is only for display purposes? My Pojos do all have a value for all variables.

@Table(name = "printing_stylesheets")
public class XsltFile implements Serializable {

     * Serialization ID
    private static final long serialVersionUID = 9092302664515365171L;

    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private long id;

    @Column(name = "xsltFile", nullable = false)
    private byte[] xsltFile;

    @Column(name = "fileName")
    private String fileName;

    @Column(name = "dateCreated", nullable = false)
    private Date dateCreated;

    public Date getDateCreated() {
        return dateCreated;

    public void setDateCreated(Date dateCreated) {
        this.dateCreated = dateCreated;

    public XsltFile() {
        dateCreated = new Date();

    public long getId() {
        return id;

    public void setId(long id) { = id;

    public byte[] getXsltFile() {
        return xsltFile;

    public void setXsltFile(byte[] xsltFile) {
        this.xsltFile = xsltFile;

    public String getFileName() {
        return fileName;

    public void setFileName(String fileName) {
        this.fileName = fileName;


I just skimmed through the post, and might be that I missed something. However, when you mentioned hibernate it reminded of a mistake I made a few days back.

I had the same sort of elements: a form factory that builds up a select from objects. My select had always ‘null’ in it even if there was a real object selected to the beanitem and a corresponding object was in the select -list. The mistake I made was I loaded (with hibernate) the list of alternatives every time the field was requested. This means that Java sees the one in my bean as a different object as the one in the select, as they have a different java object id. Saving a list of the alternatives to the fieldfactory the first time the field was loaded solved my problems. (ie. store the result of getAllXSLTs() into a local variable and use that always when needed.)

It can be that your problem may be something similar, but I can be completely wrong. I guess it depends on if getAllXSLTs() creates new instances of the objects or just gives a list of the xslt’s stored somewhere in runtime.

Thanks Jens, that was it… I was thinking that the Hibernate objects would be the same as a row in the DB, but of course they’re not. Kinda contra-intuitive but makes sense! Thanks!

You should always write hashCode() and equals() methods for your Hibernate objects. It should also solve this kind of issues.

i have the same problem.
after override equals(), the select still dose not work