Results 1 to 3 of 3
  1. #1
    ehudfisher is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default 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:

    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();
    	}
    The class that implements TreeNode:

    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);
    	}
    }
    Assuming the database logic works (it does) - what am I doing wrong?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    'children' is never null, so 'getChildrenFromDb()' is never invoked.

    db

  3. #3
    ehudfisher is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default

    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

  1. Question about a class implementing Runnable
    By d3n1s in forum Advanced Java
    Replies: 12
    Last Post: 06-18-2011, 06:16 PM
  2. NullPointerException with treeNode error
    By jasonwucinski in forum New To Java
    Replies: 3
    Last Post: 05-05-2011, 09:27 PM
  3. cast string to treeNode
    By jasonwucinski in forum Advanced Java
    Replies: 1
    Last Post: 04-26-2011, 11:51 PM
  4. equals() failing in userObject of TreeNode
    By PrinceSendai in forum AWT / Swing
    Replies: 1
    Last Post: 12-25-2010, 08:57 PM
  5. Replies: 2
    Last Post: 12-14-2010, 04:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •