Table Component: getValue vs getItem

Hi everybody!

I’ve a Table component and I set as ContainerDataSource a class that extends IndexedContainer.

[color=#969696]

    ///////////////////// main class /////////////////////////////////////////
       Table table = new Table();
       IndexedContainer p = ElementsDataContainer.getAllTasks( );   // fill  up
   addComponent(table);
	   table.setContainerDataSource(p);

     ///////////////////////// ElementsDataContainer class /////////////////////////
            IndexedContainer ic;
            Object id = ic.addItem();
        ic.getContainerProperty(id, "Process Instance Id").setValue( xxx );
        ic.getContainerProperty(id, "Definition Id").setValue(xxx);
        ic.getContainerProperty(id, "Name").setValue(xxx);
            ....

[/color]

That works fine.

Now I want to create a listener that when I click a row (item) open a new Window.
First of all I need to read selected row to know wich “kind” is it.

[color=#969696]

table.addListener(new Table.ValueChangeListener() {
public void valueChange(ValueChangeEvent event) {
Object idx = table.getValue();
Item myItem = p.getItem( idx );

[/color]

MY PROBLEM: myItem throws NullPointerException. Idx is just the index of the row in the table not an IdItem. I don’t know how it sets the id… because when I fill up I don’t determine that.
Long story short, HOW CAN I GET MY ITEM (or row info)?

I’m prooving with that code (http://vaadin.com/forum/-/message_boards/view_message/341651):
[color=#969696]

     Item item = table.getItem((Integer) event.getProperty().getValue());
 Object myObjectProperty = item.getItemProperty("Process Instance Id").getValue();

[/color]

but it throws a cast exception…
com.vaadin.event.ListenerMethod$MethodException
Cause: java.lang.ClassCastException: java.util.Collections$UnmodifiableSet cannot be cast to java.lang.Integer
at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:510)

Why getValue is returning UnmodifiableSet? :S

It must be easy…I just want to get my item :frowning:

If you want to listen to a click on a row you should implement ItemClickListener.
The ItemClickEvent then gives you the itemId for the row you clicked on.

Otherwise you have to make the table selectable.

PS: I’m a little confused about your usage of two different containers (except p and ic are the same).

Hi Sascha!

You’re right. Finally, I solved doing that:

[color=#969696]

table.addListener(new ItemClickListener () {
@Override
public void itemClick(ItemClickEvent event) {
Item item = table.getItem(event.getItemId());
Object myObjectProperty = item.getItemProperty(“myProperty”).getValue();
}

    });

[/color]

Really thx!

P.s.: ic and p are the same container. (Ic is in the class where I create it and p where I read it)

As stated in the javadoc for setValue() in AbstractSelect (superclass of Table) and in a few other locations:

If the Table is in multi-select mode, its getValue() returns a Set of item identifiers, and setValue() should be given a Set of item identifiers.
If the table is in single select mode, its value is an item identifier - which can of course be null if nothing is selected.

You then need to map from the item identifier to the item using getItem().
If you want to e.g. get a property value, you could also use directly something like (typed from memory, not tested):

Property property = table.getContainerProperty(itemId, propertyId);
Object value = (null != property) ? property.getValue() : null;