Associate an object with a table row

Hello everybody,

I am currently creating a table, consisting of three columns (an image, a label and a comboBox).

Now I would like to associate (link, bind) an object to this table row.
So that when I get a clickEvent on the specific row I want to be able to access an object (f.e. MyObject.class) that I have linked to this row?

I already tried to add a property to the table item, unfortunately this does not work as the table seems to have an HierarchicalContainer.

I would need this concept too for creating a tree and it seems like I am missing a “key concept” for associating arbitrary object with userInterface components which I can access in case of an event.

Any help is appreciated! :wink:

Thanks in Advance!
Stefan

Hi there,

Tables make use of containers to keep track of their data. So the trick is to create your own custom table and custom container and make the container extend a BeanItemContainer.

What I did in my project is create a custom table and make it do that it uses your custom container., for example:

public class CustomTable extends Table {
     private CustomContainer customContainer;

      public CustomTable () {
                customContainer = new CustomContainer();
                setContainerDataSource(customContainer);
      }
}

Then there’s also the CustomContainer class. You’ll have to keep track of the data which has been or has not been added. You can do this with a collection or so. So when you try and fill your table you’ll need a method which will read the data from the collection and add it to your container.

public class CustomContainer extends BeanItemContainer<CustomBeanItem> {
       private Collection<CustomBeanItem> collection;

       public CustomContainer () {
            super(CustomBeanItem.class);
            collection = new ArrayList<CustomBeanItem>();
       }

       public void getData() {
            this.removeAllItems();
            for (Iterator iterator = collection.iterator(); iterator.hasNext();) {
                   this.addItem(a new CustomBeanItem);
            }
       }
}

Now when you try to recieve ( for example ) the selected object from your table you can do the following:

  1. Get the BeanItem: BeanItem<CustomBeanItem> selectedObject = (BeanItem<CustomBeanItem>) table.getItem(table.getValue());
  2. Get the Bean: (CustomBeanItem) selectedObject.getBean();

Seems like I do this all the time, so it should work. Of course the BeanItem explanation also works fine as it basically does the same thing, but for you based on the Bean properties.

If not using a BeanItem, when you define your Item create a property like “MyObject” with the type of your object like MyObject.class, and you can just default it to null:


addContainerProperty(“MyObject”, MyObject.class, null);

Then when you create a new Item (via addItem), you can get that property and set it with your object, and this mostly depends a bit on whether you are supplying the property id or not:


Object pid = addItem();
Item pitem = getItem(pid);
pitem.getItemProperty(“MyObject”).setValue(myObject);

Then when you need to retrieve it:


MyObject myObject = (MyObject)item.getItemProperty(“MyObject”).getValue();

Thanks a lot for both answers!

Honestly I prefer the solution suggested by David, to add a new container property.
If I set the visible columns for the table I can even prevent this property from being displayed! ggg

Thanks again for your help!

Greetings
Stefan