2012-07-22 28 views
2

我想写一个实现TreeModel类的类。我希望有人能指引我朝着正确的方向前进。以下是我的课程。问题是当我将它绑定到一个jTree组件时,第二个级别不断被添加。所以我怀疑我的参考父对象是错误的:实现TreeModel

public class PMEntry implements TreeModel{ 

private String title; 
private List<PMEntry> pmEntryCollection; 
private String pmId; 
private String href; 
private PMEntry root; 
private ModuleType type; 

public PMEntry (PMEntry root){ 

    this.root = root; 
} 

@Override 
public Object getRoot() {   

    return ((PMEntry)this.root); 
} 

@Override 
public Object getChild(Object o, int i) { 


    if(getPmEntryCollection().isEmpty()){ 

     return null; 

    }else { 

     return (PMEntry) getPmEntryCollection().get(i); 

    } 
} 

@Override 
public int getChildCount(Object o) { 

    if(getPmEntryCollection().isEmpty()){ 

     return 0; 

    }else { 

     return getPmEntryCollection().size(); 

    } 
} 

@Override 
public boolean isLeaf(Object o) { 
    PMEntry pmentry = (PMEntry)o; 
    return (pmentry.getType() == ModuleType.DM) ? true : false; 
} 

@Override 
public void valueForPathChanged(TreePath tp, Object o) { 
    //todo 
} 

@Override 
public int getIndexOfChild(Object parent, Object child) { 

    if (!(parent instanceof PMEntry)){ 

     System.out.println("Returning -1"); 
     return -1; 
    }   

    PMEntry pParent = (PMEntry) parent; 

    List<PMEntry> children = pParent.getPmEntryCollection(); 

    if (children == null) { 
     System.out.println("children = null, Returning -1"); 
     return -1; 

    } 

    for (int i = 0; i < children.size(); i++) { 

     System.out.println("Child:" + child); 

     if (children.get(i) == child) { 

      return i; 
     } 

    } 

    return -1;   
} 

@Override 
public void addTreeModelListener(TreeModelListener tl) { 
    //todo 
} 

@Override 
public void removeTreeModelListener(TreeModelListener tl) { 
    //todo 
} 

@Override 
public String toString(){ 

    return this.getTitle(); 
} 
public enum ModuleType { 

    PM, 
    DM 

} 

// getters and setters here.... 

而这就是我如何结合数据

片段
PMEntry tm = new PMEntry(null); 
tm.setTitle("Root"); 

PMEntry pmRoot = new PMEntry((PMEntry)(tm)); 
pmRoot.setTitle("Project"); 

PMEntry pm1 = new PMEntry(pmRoot); 
pm1.setType(PMEntry.ModuleType.DM); 
pm1.setTitle("Publication Module"); 

PMEntry pm2 = new PMEntry(pmRoot); 
pm2.setType(PMEntry.ModuleType.PM); 
pm2.setTitle("Chapter");  

List<PMEntry> pmCollection = new ArrayList<PMEntry>();  
List<PMEntry> pmCollection1 = new ArrayList<PMEntry>(); 

PMEntry pm3 = new PMEntry(null); 
pm3.setType(PMEntry.ModuleType.DM); 
pm3.setTitle("Data Module"); 

PMEntry pm4 = new PMEntry(null); 
pm4.setType(PMEntry.ModuleType.DM); 
pm4.setTitle("Data Module"); 

pmCollection1.add(pm3); 
pmCollection1.add(pm4); 

pm2.setPmEntryCollection(pmCollection1); 

pmCollection.add(pm1); 
pmCollection.add(pm2);      

pmRoot.setPmEntryCollection(pmCollection); 

this.jTree1.setModel(pmRoot); 

回答

4

我不知道为什么你认为你需要实现TreeModel 。你有没有看过DefaultTreeModel?你计划实施什么样的新行为?

+0

嗨,我想保存我的对象。我发布的示例是一个较大对象的虚拟版本,它希望能够创建,绑定和编辑它。 – PhillyNJ 2012-07-22 00:44:38

+0

不是你应该考虑的答案:你的对象与DefaultTreeModel有什么不同?您可以创建,编辑和绑定它。 – duffymo 2012-07-22 00:48:31

+0

嗨,我不只是想显示一个节点或一个叶节点,我想代表我的对象的TreeModel和其对对象的引用。 – PhillyNJ 2012-07-22 00:56:11

2

我必须同意@duffymo & @HFOE:不要过早拒绝DefaultTreeModel。有一个示例here,其示出了用于编辑userObjectnameTreeCellEditor

如果你真的需要实施TreeModel,FileTreeModel,讨论here,是一个相当易于访问的例子。

相关问题