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);
안녕하세요, 내 개체를 보존하고 싶습니다. 내가 게시 한 예제는 더 큰 오브젝트의 벙어리 다운 버전으로, 생성하고 바인드하고 편집 할 수 있기를 원합니다. – PhillyNJ
당신이 생각해야 할 대답이 아닙니다 : 당신의 객체는 DefaultTreeModel과 어떻게 다른가요? 그것을 만들고, 편집하고, 바인딩 할 수 있습니다. – duffymo
안녕하세요, 그냥 노드 또는 리프 노드를 표시하고 싶지 않아, 내 개체의 TreeModel 및 개체에 대한 참조를 나타내려고합니다. – PhillyNJ