combobox in form

Hi:

I have two mysql tables. One of them is the source for a form, and the other one is the source for a combobox in that same form, created using
DefaultFieldFactory
. In the first table I have a column that references the Id column of the second table, an integer in both tables. In the combobox, I use
setItemCaptionPropertyId
to show the property of the table I want to show in the combobox (not the id).

When I create the form, I do it as a read only form, and I get only the border of the combobox and no value in it ( the rest of the properties work fine)…when I set the form to editable mode, I can choose the value I want from the combobox, but when commiting the form it doesn’t change that value…

Here is the code for creating the form:

form.setItemDataSource(containerSettings.getItem(table.getValue()));            //I get the item from other table, this works perfect
				form.setFormFieldFactory(new SettingsFieldFactory(containerSettings.getItem(table.getValue())));
				form.setReadOnly(true);

…here is the field factory:

private class SettingsFieldFactory extends DefaultFieldFactory {
		
		final ComboBox user = new ComboBox("Owner");

		private static final String COMMON_FIELD_WIDTH = "12em";
	
		 public SettingsFieldFactory(Item item) {
			 
			 user.setContainerDataSource(containerUsers);
			 user.setItemCaptionPropertyId("userName");  //so I don't show the id, but the name of the user
			 user.setNullSelectionAllowed(false);
		 }
		 
		 public Field createField(Item item, Object propertyId,
		            Component uiContext) {
		        Field f;
		        
		        if ("Owner".equals(propertyId)) {
		        	user.setWidth(COMMON_FIELD_WIDTH);
		        	user.setRequired(true);
		        	user.setRequiredError("This field must be completed");
		            return user;
		        } else {               
		        	f = super.createField(item, propertyId, uiContext);	
		        } 
		        return f;
			}
	}

and here is what the “save” button does:

form.commit();

				try {
					containerSettings.commit();
				} catch (UnsupportedOperationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				form.setReadOnly(true);
			}

wherever I try to set the value for the combobox I get a read only error…If I don’t set the value for the combobox, everything works fine, except that no value is shown in the combobox… when I change the form to editable mode I can choose a value from the combobox, I commit and change to read only, and it still shows that value but when returning to the form or checking the database the value didn’t change…this only happens with that field

any idea why this happens?..

Any idea on this???

…When using the field factory to include a combobox in a form I don’t know how to set the value of the combobox to the actual value in the item data source, spacially when in read only mode…(both the item data source and the combobox data source are SQL tables)…

thanks!!!

Hi Hugo,

Try to set your ComboBox in immediate mode. If this doesn’t work maybe you should remove your final modifier form user ComboBox instantiation.

The SettingsFieldFactory should look like:


private class SettingsFieldFactory extends DefaultFieldFactory {
        
        [b]
ComboBox user = new ComboBox("Owner");
[/b]

        private static final String COMMON_FIELD_WIDTH = "12em";
    
         public SettingsFieldFactory(Item item) {
             
             user.setContainerDataSource(containerUsers);
             user.setItemCaptionPropertyId("userName");  //so I don't show the id, but the name of the user
             user.setNullSelectionAllowed(false);
         }
         
         public Field createField(Item item, Object propertyId,
                    Component uiContext) {
                Field f;
                
                if ("Owner".equals(propertyId)) {
                    user.setWidth(COMMON_FIELD_WIDTH);
                    user.setRequired(true);
                    [b]
user.setImmediate(true);
[/b]
                    user.setRequiredError("This field must be completed");
                    return user;
                } else {               
                    f = super.createField(item, propertyId, uiContext);    
                } 
                return f;
            }
    }

HTH,

Javi

A field factory should not try to set the value of a field even though it can otherwise configure the fields.
The framework will later call setValue() based on the values in the properties of the item, overwriting anything you might have set in the field factory.

Read-only mode is somewhat special, though, as it may also block the later automatic setValue(). Having the underlying item property as read-only would probably work, but not sure if you can do it with SQLContainer - at least without subclassing it.

Hi Javier…I tried what you say here but I still get an empty rectangle when the form is in read only form, and the combobox with no value selected when not in read only mode…

Should this work exactly like that when using SQLContainers?..any other idea on why this is not working?..In fact, when I use the combobox, I commit the form, it does change the value in the MySQL table, but when I return to read only mode it erases the value from the combobox (not the table) and displays only the rectangle…any idea why?

regards, and thank you very much…

Hugo

Hi:

I’ve done all that this thread says and nothing yet, I still get an empty rectangle when the form is in read only mode and a combobox with no selected value when editable…

something funny…when testing different options I added an input prompt that, when setting the form to read only, it is shown as a value inside the rectangle, the created combobox also dissappears when creating a second form with the same parameters (item and fieldfactory)…when the source for the items in the combobox are added manually, the value appears perfect, but it is still shown inside a rectangle when in read-only mode…any clues?..(both my tables are SQLContainers, the item source for the form and the source for the combobox)

thanks!!!

Hello,

Does anybody already know how to solve this issue?
I try to do the same thing: a foreign key lookup based on a select/combo and spent a whole day to accomplish this without any result.
I have read several of the same kind of issue on this forum, all without any solution.
This is quite a common functionality and should not be that hard.

So I understand: in the FormFieldFactory you can override the default fields.
I have done this and get a perfect select/combo. It is filled with the correct items of my SQLContainer.
When I select a value, the change is coreectly processed.
So far so good. However, it never shows the actual value!!!

I hope anybody have done this before.

Best Regards,

If you want to set actual value when you are setting item in Form to edit, you have to override
setItemDataSource
.

I created simple application where I’m using SQLContainer and foregin keys with ComboBox:

https://github.com/nonameplum/HelloWorld

Interesting part for you would be
https://github.com/nonameplum/HelloWorld/blob/master/src/com/example/helloworld/ComplexForm.java

Although I implemented setItemDataSource method and I had problem with ComboBox that not setting value from edited item, and that was the problem of type mismatch. So when you override setItemDataSource method, be shure that in select method of ComboBox are you setting proper value type, becouse select method parameter type is
Object
(is tolerant) and probably will not throw any exception when you set inprorper value and if you debug select method you will see that ComboBox will do nothing if not find object in its DataSource.

2 years ago I got same problem!!!

upss, it was my bad. I recently switched to Orientdb from Hibernate, where the id is string (not long like before), therefore my equals method (long1==long2) stopped working. I fixed it and added .setBuffered(true); and the combo works as expected.