I’m learning my way around Java JTree’s but I have this little problem I can’t figure out.
Using a tutorial I found on Oracle’s site, I’ve mimicked a tree structure they demonstrate. The problem is, I want the Folder “websites” to be an empty folder, but JTree is displaying it as though it is a file. How can I tell the JTree that “websites” is in fact an empty folder?
UPDATE
I’ve started working on a simple ‘contact manager’. What I want to do basically it make the name look like folders (since I want to add info to each of them), but without adding stuff. i.e. Some might not have any info in them, but I would still like them to look like folders.

Code for the Browser class:
import java.awt.Component;
import java.io.File;
import java.util.Iterator;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
public class Browser extends JFrame implements javax.swing.tree.TreeModel {
private JTree tree;
ManagerOfContacts mngrOfContacts;
public Browser() {
//Generates the ManagerOfContacts and associated Contacts
Driver driver = new Driver();
mngrOfContacts = driver.getManagerOfContacts();
//---------------------------\\
DefaultMutableTreeNode contacts = new DefaultMutableTreeNode("Contacts");
createNodes(contacts);
tree = new JTree(contacts);
JScrollPane treeView = new JScrollPane(tree);
add(treeView);
setSize(400,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public void createNodes(DefaultMutableTreeNode top){
DefaultMutableTreeNode contactName;
Iterator<Contact> contactItr = mngrOfContacts.getIterator();
while(contactItr.hasNext()){
contactName = new DefaultMutableTreeNode(contactItr.next());
top.add(contactName);
}
}
public static void main(String[] args) {
Browser browsr = new Browser();
}
}
This is how you create your own
TreeCellRendererwhich tells yourJTreewhatever you want. Since you are using a default tree model withDefaultMutableTreeNodeas nodes you have to extract their user object and decide what to paint based on that. Note that the default renderer is just an extendedJLabel, which is why you can usesetIcon(...),setText(...), etc. inside it.The reason why your leaf nodes are painted with file icons is probably that the default renderer chooses the icon based on
DefaultMutableTreeNode.getAllowsChildren()similarily to how I useContact.isSomeProperty().This should get you started. You should read more about this in a JTree tutorial.