Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JTree

Status
Not open for further replies.

rekenaar

Technical User
Feb 16, 2005
38
ZA
Hello

I am trying to use JTree to represent a file structure. It reads the files in a folder and put it in the tree. The Problem is if a folder is empty is not displayed correctly in the JTree (It gets displayed as a leaf).

Is there some way to force a node not be displayed as a leaf?

Here is some code:

Code:
	public void buildNodes(DefaultMutableTreeNode parentNode)
	{

		DefaultMutableTreeNode node = null;
		DefaultMutableTreeNode mainNode = null;
		String projRoot = m_parent.getFileRoot() + m_projID + "\\";
		File dir;
		String[] files;
		
		/* Opportunity */
		mainNode = new DefaultMutableTreeNode(new ModuleNode("Opportunity"));
		
		dir = new File(projRoot + "Opportunity\\");
    
    
    
    files = dir.list();
    if (files == null) {
        // Either dir does not exist or is not a directory
    } else {
        for (int i=0; i<files.length; i++) {
            // Get filename of file or directory
            node = new DefaultMutableTreeNode(new ModuleNode(files[i]));
						mainNode.add(node);
        }
    }		
	
		parentNode.add(mainNode);
		
		/* Initiation */
		mainNode = new DefaultMutableTreeNode(new ModuleNode("Initiation"));
		dir = new File(projRoot + "Initiation\\");
    
    files = dir.list();
    if (files == null) {
        // Either dir does not exist or is not a directory
    } else {
        for (int i=0; i<files.length; i++) {
            // Get filename of file or directory
            node = new DefaultMutableTreeNode(new ModuleNode(files[i]));
						mainNode.add(node);
        }
    }		
		
		parentNode.add(mainNode);

Thanks in advance, any help will be appreciated.
 
Hehe, gone for your own JFileChooser I see then !

Instead of working out how not to display a leaf, can you not just say :

Code:
if (files == null || files.length == 0) {
   ...
} else {
   ...
}

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top