Hernan1
(Hernan B)
November 25, 2010, 3:16am
1
Hello Everybody !!!
I’m very new in vaadin (congratulations for the authors!!).
I ve a question about the Table, Items, Properties classes and Listeners. I’m trying to capture a row data (item) added to a Table.
The way I’ve seen a way to add an item to a table is using something like this:
Object id = table.addItem();
Item item = table.getItem(id);
Property p_field1 = (Property) item.getItemProperty("field1");
Property p_field2 = (Property) item.getItemProperty("field2");
p_field1.setValue("value5");
p_field2.setValue("value3");
table.setValue(id);
I would like to add listener to capture an event when a row is added and all fields have values (if they were filled), but using the above way if I create a ValueChangeListener is executed twice, in the first call only the field1 has a value and in the second call values for field1 and field2 are available.
I want one call with all row data. Is possible to set all values in one call (something like item.setValues(p_field1, p_field2)? I think I can solve my problem using other code than the above (but I didn’t found it yet).
Here is my example:
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Layout;
import com.vaadin.ui.Table;
public class SimpleExample {
IndexedContainer container = new IndexedContainer();
Table table = new Table();
public Layout example() {
container.addContainerProperty("field1", String.class, null);
container.addContainerProperty("field2", String.class, null);
container.addListener(new Property.ValueChangeListener() {
@Override
public void valueChange(ValueChangeEvent event) {
// TODO Auto-generated method stub
System.out.println("ValueChangeListener::Event: "+event);
Object id = container.lastItemId();
Item item = container.getItem(id);
Property p_field1 = item.getItemProperty("field1");
Property p_field2 = item.getItemProperty("field2");
System.out.println("Property: "+event.getProperty());
System.out.println("ValueChangeListener: field1: "+p_field1.getValue());
System.out.println("ValueChangeListener: field2: "+p_field2.getValue());
}
});
table.setWidth("100%");
table.setHeight("100%");
table.setContainerDataSource(container);
Object id = table.addItem();
Item item = table.getItem(id);
Property p_field1 = (Property) item.getItemProperty("field1");
Property p_field2 = (Property) item.getItemProperty("field2");
p_field1.setValue("value1");
p_field2.setValue("value2");
table.setValue(id);
Layout layout = new HorizontalLayout();
layout.addComponent(table);
return layout;
}
}
OUTPUT[code]
Property:
ValueChangeListener::Event: com.vaadin.data.util.IndexedContainer$PropertyValueChangeEvent[source=value1]
Property: value1
ValueChangeListener: field1: value1
ValueChangeListener: field2: null
ValueChangeListener::Event: com.vaadin.data.util.IndexedContainer$PropertyValueChangeEvent[source=value2]
Property: value2
ValueChangeListener: field1: value1
ValueChangeListener: field2: value2
[/code]
Thanks in advance!!
Hernán
Lawal
(Lawal Olufowobi)
November 25, 2010, 8:08am
2
May be what you can do is,
private class TableListener implements ValueChangeListener {
private static final long serialVersionUID = 1L;
@Override
public void valueChange(ValueChangeEvent event) {
// If( no nulls in the fields ){
//proceed & do sth.
}
}
}
Olufowobi Lawal
Henri2
(Henri Sara)
November 25, 2010, 8:27am
3
I ve a question about the Table, Items, Properties classes and Listeners. I’m trying to capture a row data (item) added to a Table.
The way I’ve seen a way to add an item to a table is using something like this:
Object id = table.addItem();
Item item = table.getItem(id);
Property p_field1 = (Property) item.getItemProperty("field1");
Property p_field2 = (Property) item.getItemProperty("field2");
p_field1.setValue("value5");
p_field2.setValue("value3");
table.setValue(id);
I would like to add listener to capture an event when a row is added and all fields have values (if they were filled), but using the above way if I create a ValueChangeListener is executed twice, in the first call only the field1 has a value and in the second call values for field1 and field2 are available.
I want one call with all row data. Is possible to set all values in one call (something like item.setValues(p_field1, p_field2)? I think I can solve my problem using other code than the above (but I didn’t found it yet).
You could try some other container - BeanItemContainer could be a good fit for your needs, although its ItemSetChangeEvents do not indicate which items were added/removed. You might need your own subclass which e.g. overrides addBean() and calls your own listeners.
Another alternative with IndexedContainer, if you always fill the fields in order, would be to only react to changes in the last column.
Hernan1
(Hernan B)
November 27, 2010, 6:52pm
4
Hi Lawal,
Thanks for your response.
But if a user enters data for field1 and doesn’t enter data for field2 in a GUI Form I want to process the data. How can I differentiate in this situations?
Hernan1
(Hernan B)
November 27, 2010, 6:58pm
5
I ve a question about the Table, Items, Properties classes and Listeners. I’m trying to capture a row data (item) added to a Table.
The way I’ve seen a way to add an item to a table is using something like this:
Object id = table.addItem();
Item item = table.getItem(id);
Property p_field1 = (Property) item.getItemProperty("field1");
Property p_field2 = (Property) item.getItemProperty("field2");
p_field1.setValue("value5");
p_field2.setValue("value3");
table.setValue(id);
I would like to add listener to capture an event when a row is added and all fields have values (if they were filled), but using the above way if I create a ValueChangeListener is executed twice, in the first call only the field1 has a value and in the second call values for field1 and field2 are available.
I want one call with all row data. Is possible to set all values in one call (something like item.setValues(p_field1, p_field2)? I think I can solve my problem using other code than the above (but I didn’t found it yet).
You could try some other container - BeanItemContainer could be a good fit for your needs, although its ItemSetChangeEvents do not indicate which items were added/removed. You might need your own subclass which e.g. overrides addBean() and calls your own listeners.
Another alternative with IndexedContainer, if you always fill the fields in order, would be to only react to changes in the last column.
Hi Henri,
Thanks for your response.
Interesting the use of BeanItemContainer, but I’ll lost the IndexedContainer capabilities. Does exists a something similar to “addBean” in an IndexedContainer?
I think that I’ll use the order as you think, and try to remember forever this trick to consider when I want to append new fields.
Hernan1
(Hernan B)
November 27, 2010, 8:31pm
6
I ve a question about the Table, Items, Properties classes and Listeners. I’m trying to capture a row data (item) added to a Table.
The way I’ve seen a way to add an item to a table is using something like this:
Object id = table.addItem();
Item item = table.getItem(id);
Property p_field1 = (Property) item.getItemProperty("field1");
Property p_field2 = (Property) item.getItemProperty("field2");
p_field1.setValue("value5");
p_field2.setValue("value3");
table.setValue(id);
I would like to add listener to capture an event when a row is added and all fields have values (if they were filled), but using the above way if I create a ValueChangeListener is executed twice, in the first call only the field1 has a value and in the second call values for field1 and field2 are available.
I want one call with all row data. Is possible to set all values in one call (something like item.setValues(p_field1, p_field2)? I think I can solve my problem using other code than the above (but I didn’t found it yet).
You could try some other container - BeanItemContainer could be a good fit for your needs, although its ItemSetChangeEvents do not indicate which items were added/removed. You might need your own subclass which e.g. overrides addBean() and calls your own listeners.
Another alternative with IndexedContainer, if you always fill the fields in order, would be to only react to changes in the last column.
Henri,
How can I know that the the call is for the last column since event.getProperty() in a callback method returns the VALUE of the field instead of the field name (in this case “value2” instead of “field2”).
Thanks!!