Data from SQLContainer to Combobox?

Hi!

What is the best or easiest way to get values from SQLContainer to Combobox?

FreeformQuery free = new FreeformQuery(queryString, connectionPool);
countryContainer = new SQLContainer(free);

Table countryTable = new Table(“countries”, dbHelper.getCountryContainer());
tabLayout5.addComponent(countryTable);

So what is the best way to populate combobox with that data and how?

Thanks!
Sami

FreeformQuery free = new FreeformQuery(queryString, connectionPool);
countryContainer = new SQLContainer(free);
combobox.setContainerDataSource(countryContainer);

I hope that it will be usefull :slight_smile:

I tried that already, but no success. I got that error message:

java.lang.NullPointerException
at com.vaadin.addon.sqlcontainer.RowId.toString(RowId.java:70)
at com.vaadin.ui.AbstractSelect.getItemCaption(AbstractSelect.java:1087)
at com.vaadin.ui.Select.paintContent(Select.java:245)
at com.vaadin.ui.ComboBox.paintContent(ComboBox.java:98)
at com.vaadin.ui.AbstractComponent.paint(AbstractComponent.java:755)
at com.vaadin.ui.AbstractOrderedLayout.paintContent(AbstractOrderedLayout.java:171)
at com.vaadin.ui.AbstractComponent.paint(AbstractComponent.java:755)
at com.vaadin.ui.TabSheet.paintContent(TabSheet.java:404)
at com.vaadin.ui.AbstractComponent.paint(AbstractComponent.java:755)
at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.writeUidlResponce(AbstractCommunicationManager.java:954)
at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.paintAfterVariableChanges(AbstractCommunicationManager.java:841)
at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.doHandleUidlRequest(AbstractCommunicationManager.java:767)
at com.vaadin.terminal.gwt.server.CommunicationManager.handleUidlRequest(CommunicationManager.java:296)
at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service(AbstractApplicationServlet.java:501)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

Data is like that if I populate table with it:

So have you an idea what is happening. I am too newbie with Vaadin so I have to ask. I tried just select countries in my select (only one column) but I got the same result :frowning:

Thanks for helping me!
Sami

What is the query queryString?

String queryString = “select * from CITY”;

I tried everything like select cityid from CITY, select * from countries etc. Everything is working fine with Table, but not in Combobox. BTW how can I get key/value pairs from SQLContainer?

PropertyIDs I can iterate like that, but can I have keys and values inside the container? I think that it is the another possibility to build working Combobox?
Collection propertyIds = countryContainer.getContainerPropertyIds();
Iterator iterator = propertyIds.iterator();
while (iterator.hasNext()) {

Thanks a lot to you!
Sami

I have no idea … sorry :confused:
I’ll give you an example of how i used the combobox with sqlcontainer, maybe it will help you.

This is my product Table in mySQL:

| id | name | value | year |


and this is the code to populate the combo:

try {
  String query1 = new String("SELECT * FROM product");
  FreeformQuery productFreeFormQuery = new FreeformQuery(query1, myConnection.getConnectionPool());
  productContainer = new SQLContainer(productFreeFormQuery);
} catch (SQLException e) {
  e.printStackTrace();
}
productComboBox.setContainerDataSource(productContainer);
productComboBox.setItemCaptionPropertyId("name");
productComboBox.setNullSelectionAllowed(false);
productComboBox.setImmediate(true);

productComboBox.addListener(
   new Property.ValueChangeListener() {
    public void valueChange(ValueChangeEvent event) {
     Item item = productComboBox.getItem(productComboBox.getValue());
     String selectedProductString = item.getItemProperty("id").toString();
     getWindow().showNotification("Selected Item: " + selectedProductString);
   }
 }
);

In this way i see the column “name” in the combobox, but on click i select the value “id”.

ComboBox combobox = new ComboBox();
combobox.setContainerDataSource(dbHelper.getCountryContainer());
combobox.setItemCaptionPropertyId(“country”);---------------> I added this one and it worked, I have no idea what it is doing :slight_smile:
combobox.setNullSelectionAllowed(false);
combobox.setImmediate(true);

Thanks a lot for your help!! BTW could I ask you a little bit more :slight_smile:

I have a form where is about 8 fields, couple of comboboxes and 6 text fields. User is adding a new company to my system. What is the wisest way to make that kind of form?

Just new form and new text Fields and after submiting the form populate my compamyPOJO with my data field by field? There is nested pojos (companyPOJO includes AddressPOJO and AddressPOJO includes cityPOJO etc.)

basicInfoForm = new Form();
	TextField companyName = new TextField("Company name"); //This goes to companyPOJO and COMPANY-table
	TextField address = new TextField("Address");//This goes to AddressPOJO inside the CompanyPOJO and ADDRESS-table
	
	TextField zip = new TextField("Zip");//This goes to AddressPOJO inside the CompanyPOJO and ADDRESS-table
	ComboBox combobox = new ComboBox("Select country");
	combobox.setContainerDataSource(dbHelper.getCountryContainer());
	combobox.setItemCaptionPropertyId("country");
	combobox.setNullSelectionAllowed(false);
	combobox.setImmediate(true); 
	
	
	basicInfoForm.addField("companyName",companyName);
	basicInfoForm.addField("address", address);
	basicInfoForm.addField("zip", zip);
	basicInfoForm.addField("combobox", combobox);
	
	basicInfoForm.getLayout().addComponent(new TextField("TEST"));

	
	Button okbutton = new Button("OK", basicInfoForm, "commit");
	okbutton.addListener(new ClickListener() {....

When comminting that form, form is null. I don’t know how to use form.addField-method. I don’t know how to bind SQLContainer to Form, I don’t know how to bind one field to my CompanyPOJO’s field, for instance address etc.

Could you pls give me some example or explain what is the wisest way to do that. Another case is updating, then (I think) I have to bind updated fields to SQLContainer’s field or ComapnyPOJO’s fields and then I have no idea how to handle nested POJOs (CompanyPOJO-AddressPOJO).

Thank you very much!
Sami

Hi,

Stumbled upon this thread when investigating a problem where the Combobox backed by a SQLContainer would not show the Item value.
The situation is that I have a Form, backed by an Item datasource. One of the Item properties is a Long called
Material.SUPPLIER_ID_DB

The suppliers are loaded from the database via a SQLContainer.

If I want to edit the item, the Combobox is not showing the correct supplier. NB It correctly loads the suppliers from the database.
After some digging i found that this piece of code solves my problem.


Object data = materialItem.getItemProperty( Material.SUPPLIER_ID_DB ).getValue();
((com.vaadin.ui.ComboBox) getField( Material.SUPPLIER_ID_DB )).select(new RowId(new Object[] { data }));

Kind regards,
Harke

how to select value from combobox when it views SQLContainer data

SQLContainer container=null;
        
        TableQuery tq = new TableQuery(table, Db.pool);
        try {
            container= new SQLContainer(tq);
            
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
final ComboBox cm1 = new ComboBox("Salary Config");
cm1.setContainerDataSource(container);

cm1.setValue("temp");