Results 1 to 3 of 3
- 08-14-2011, 06:11 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 4
- Rep Power
- 0
JTree - Implementing TreeNode question
Hi,
I'm trying to use a JTree as a UI to a database table (the lazy way - only querying what i need when i need it). For some reason, though I implemented all the methods needed, The JTree only displays the root and nothing else.
creating th JTree:
The class that implements TreeNode:Java Code:public class LibraryBrowser extends JFrame { private static final long serialVersionUID = 1L; private TreeModel dtm; private JTree tree; private DBUtil db; public LibraryBrowser() { setDb(new DBUtil(this)); db.start(); dtm = new DefaultTreeModel(getRoot(), true); tree = new JTree(dtm); add(new JScrollPane(tree)); } private LibEntity getRoot() { return db.getTop(); }
Assuming the database logic works (it does) - what am I doing wrong?Java Code:public class LibEntity implements TreeNode{ private static final long serialVersionUID = 1L; private Integer id = null; private Integer parentId = null; private String Title = null; private Double Max = null; private Double Min = null; private String Comment; private LibraryBrowser lb; private LibEntity parent; private List<LibEntity> children = new ArrayList<LibEntity>(); //private DefaultMutableTreeNode treeNode; public LibEntity(LibraryBrowser lb, Integer id, Integer parentID, String Title, String Comment, Double Min, Double Max) { super(); this.lb = lb; this.id = id; this.parentId = parentID; this.Title = Title; this.Comment = Comment; this.Min = Min; this.Max = Max; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; LibEntity libEntity = (LibEntity) o; if (id != null ? !id.equals(libEntity.id) : libEntity.id != null) return false; return true; } @Override public int hashCode() { return id != null ? id.hashCode() : 0; } @Override public String toString() { return Title; } private void getChildrenFromDb() { children = lb.getDb().getChildren(id); for (LibEntity e : children) e.setParent(this); } @Override public Enumeration children() { System.out.print("children "); if (children==null) getChildrenFromDb(); Vector<TreeNode> vec = new Vector<TreeNode>(); for(LibEntity ent : getChildren()){ vec.add(ent); } return vec.elements(); } @Override public TreeNode getParent(){ System.out.print("parent "); return parent; } @Override public boolean getAllowsChildren() { System.out.print("allowschildren "); return !isLeaf(); } @Override public boolean isLeaf() { System.out.print("isleaf "); return ((Min>=0) ||(Max>=0)); } @Override public TreeNode getChildAt(int childIndex) { System.out.print("getChildAt "); return children.get(childIndex); } @Override public int getChildCount() { return children.size(); } @Override public int getIndex(TreeNode node) { System.out.print("getIndex "); return children.indexOf(node); } }
- 08-16-2011, 09:40 AM #2
'children' is never null, so 'getChildrenFromDb()' is never invoked.
db
- 08-16-2011, 10:23 AM #3
Member
- Join Date
- Jul 2011
- Posts
- 4
- Rep Power
- 0
Well.. that wasn't the problem (It is a problem, just one that I noticed and solved, just pasted an older version of the code..).
The real problem was that the implementation of getChildCount, is called before "children()", and therefore worked on a null "children" version. I did not see that, because somehow I forgot to put there the debug print that I put in the rest of the overriden methods.
Thanks!
Similar Threads
-
Question about a class implementing Runnable
By d3n1s in forum Advanced JavaReplies: 12Last Post: 06-18-2011, 06:16 PM -
NullPointerException with treeNode error
By jasonwucinski in forum New To JavaReplies: 3Last Post: 05-05-2011, 09:27 PM -
cast string to treeNode
By jasonwucinski in forum Advanced JavaReplies: 1Last Post: 04-26-2011, 11:51 PM -
equals() failing in userObject of TreeNode
By PrinceSendai in forum AWT / SwingReplies: 1Last Post: 12-25-2010, 08:57 PM -
Generic question on implementing bluetooth/net code
By SM2010 in forum New To JavaReplies: 2Last Post: 12-14-2010, 04:10 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks