0

I am currently creatin a TreeView where leaf elements should be checkable.

I created the sampleTreeView from the eclipse plugin that comes with a predefined Tree structure.

public class TreeObject {
    private String name;
    private TreeParent parent;

    public TreeObject(String name) {
       this.name = name;
     }
     public String getName() {
        return name;
     }
     public void setParent(TreeParent parent) {
         this.parent = parent;
     }
     public TreeParent getParent() {
        return parent;
     }
     public String toString() {
        return getName();
     }
     public Object getAdapter(Class<?> key) {
    return null;
    }
}


public class TreeParent  extends TreeObject{
     private ArrayList<TreeObject> children;
     public TreeParent(String name) {
        super(name);
         children = new ArrayList<TreeObject>();
     }
     public void addChild(TreeObject child) {
         children.add(child);
         child.setParent(this);
     }
     public void removeChild(TreeObject child) {
         children.remove(child);
         child.setParent(null);
   }
     public TreeObject [] getChildren() {
         return (TreeObject [])children.toArray(new TreeObject[children.size()]);
   }
      public boolean hasChildren() {
         return children.size()>0;
   }
} 

I then found the following tutorial. He is using the TreeItem Object where it is easy to attach a Image. Can I somehow Copy this function or do I have to use the TreeItem object as my data structure?

Basilevs
  • 22,440
  • 15
  • 57
  • 102
Ogofo
  • 356
  • 2
  • 6
  • 13
  • possible duplicate of [JTree with checkboxes](http://stackoverflow.com/questions/1223188/jtree-with-checkboxes) – nashuald Nov 19 '13 at 11:45
  • @nashuald This question is about jface class TreeViewer, while you are pointing to JTree which is from swing. – Basilevs Jan 27 '14 at 17:32

1 Answers1

0

The tutorial says at the beginning of part 2 that you should use a ContentProvider and LabelProvider for the TreeViewer rather than use TreeItem, and that is what you should do.

The getImage method of the label provider would return the checked / unchecked /null image.

When you need to change an image call TreeViewer.update or TreeViewer.refresh if the children of the object also need refreshing. This will call the label provider again.

greg-449
  • 109,219
  • 232
  • 102
  • 145
  • But how do I call the LabelProvider once it set the inital images? – Ogofo Nov 19 '13 at 12:30
  • Or rather call [fireLabelProviderChanged](http://help.eclipse.org/indigo/nftopic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/jface/viewers/BaseLabelProvider.html#fireLabelProviderChanged%28org.eclipse.jface.viewers.LabelProviderChangedEvent%29) – Basilevs Jan 27 '14 at 17:34