how to get parent of an item in a tree

Hi,

I think this is an easy question and I’m just missing something - how do I get the parent of an item when the user clicks on an element in a tree?

The following doesn’t work because


  private ItemClickListener doClickCategory() {
    return new ItemClickListener() {
      @Override
      public void itemClick(ItemClickEvent event) {
        Tree tree = (Tree) event.getSource();
        Item categoryItem = event.getItem();
        Item quarterItem = (Item) tree.getParent(categoryItem);
        
        // get the content from the database
        String content = "";
        contentLabel.setContentMode(Label.CONTENT_XHTML);
        //contentLabel.setValue(content);
        contentState.showContent(true);
      }
    };
  }

(Item) tree.getParent(categoryItem);

categoryItem is supposed to be the itemId and not the actual item.

Funny thing is, I can’t see a way to get the itemId from the Item or via the Container itself. I actually would have expected the Item to know its own ID - do I have to manually add that as a property myself? I know how to do that, but seems odd to have to do so…

Its the end of the day here, I’m assuming I’m just missing something…

Thanks,

Mark

Use ItemClickEvent.getItemId().

An item does not necessarily know its id, nor even have one - the id is what identifies the item in a particular container instance, but need not be an intrinsic property of the item. Some containers use item ids based on the content of the item, others don’t - or cannot.