Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Better way to trigger update of TreeItem?

john16384Dec 4 2011 — edited Dec 6 2011
I have a TreeView which consists of thumbnails -- these get loaded on-demand and in the mean time, just some text is displayed.

I'm however having trouble getting the TreeView to recognize when such an item is updated. Initially I tried this, calling setValue on the appropriate TreeItem:
         private void updateItem(MediaItem item) {
      TreeItem<MediaItem> foundItem = findMediaItem(treeRoot, item);
      
      if(foundItem != null) {
        foundItem.setValue(item);
      }
    }

    private TreeItem<MediaItem> findMediaItem(TreeItem<MediaItem> treeRoot, MediaItem mediaItem) {
      for(TreeItem<MediaItem> treeItem : treeRoot.getChildren()) {
        if(treeItem.getValue().equals(mediaItem)) {
          return treeItem;
        }
        
        if(!treeItem.isLeaf()) {
          TreeItem<MediaItem> foundItem = findMediaItem(treeItem, mediaItem);
          
          if(foundItem != null) {
            return foundItem;
          }
        }
      }
      
      return null;
    }
Unfortunately, I could not get that to work at all. After some more experimentation I found a way that does work, but it is very cumbersome:
 private void updateItem(MediaItem item) {
      TreeItem<MediaItem> foundItem = findMediaItem(treeRoot, item);
      
      if(foundItem != null) {
        int index = foundItem.getParent().getChildren().indexOf(foundItem);
        
        foundItem.getParent().getChildren().set(index, new TreeItem<MediaItem>(item));
//        foundItem.setValue(item);
      }
    }
Having to create a new TreeItem object and looking for the index of the old object just so I can replace it in the ObservableList of its parent seems overkill. I was more looking for a simple "updateTreeItem" method or something, where I can pass the item that was updated. The TreeCell API does seem to have this method, but I donot see how I can use it.

Note: the "new" item I'm updating here is actually the same instance (in other words, it is equal to the previous one) -- I only want to trigger a redraw, as its look has changed even though it is still equal. I haven't tried overriding equals for it, but even if that does work it would be unsatisfactory as, from an application standpoint, these two items are equal apart from having a thumbnail that finished loading...

Any simpler solution?

Edited by: john16384 on Dec 4, 2011 7:40 PM
This post has been answered by zonski on Dec 4 2011
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 3 2012
Added on Dec 4 2011
2 comments
3,697 views