Sunday, May 11, 2008

Using TreeItemRenderer and callLater()

A friend of mine needed a Tree Component which had images to the right of text labels and also which would have some items disabled.



The simplest way to go about it was to use a custom ItemRenderer for the Tree component. Here is how I tried to solve it...

  1. Extend the TreeItemRenderer class to be used for the Renderer.
  2. Override the createChildren method and add an image component within the renderer.
  3. Access the listData variable and cast it to TreeListData to know whether a node has further subnodes,etc. and then attach the image.
  4. Override the set Data function to check if a node needs to be disabled / enabled and accordingly set the style.
Well, I thought my work was done...But, there still was a problem, the disabled look behaved rather inconsistently going on / off randomly. Here is how I solved the issue.

  1. Use the callLater function to set the enabled/ disabled style. The callLater function defers the calling of the given function until the end of the cycle.
You can check the example here (view source enabled).

1 comments:

SomeoneSomewhere said...

Hey aasim,
your blog entry helped and saved lot of time...I needed exactly something like this. I had to put a checkbox for selecting a particular item in the tree and I was able to do that with itemeditor added on top of your code..:)

thanks!