Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8956621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:49:42+00:00 2026-06-15T14:49:42+00:00

I’m learning my way around Java JTree’s but I have this little problem I

  • 0

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.

enter image description here

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();
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:49:43+00:00Added an answer on June 15, 2026 at 2:49 pm

    This is how you create your own TreeCellRenderer which tells your JTree whatever you want. Since you are using a default tree model with DefaultMutableTreeNode as nodes you have to extract their user object and decide what to paint based on that. Note that the default renderer is just an extended JLabel, which is why you can use setIcon(...), 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 use Contact.isSomeProperty().

    import java.awt.Component;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTree;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.DefaultTreeCellRenderer;
    
    
    public class Browser extends JFrame {
    
    
        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);
    
            // use your own renderer (!)
            tree.setCellRenderer(new MyTreeCellRenderer());
    
            JScrollPane treeView = new JScrollPane(tree);
    
            add(treeView);
    
            setSize(400,400);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setVisible(true);
        }
    
        public final void createNodes(DefaultMutableTreeNode top){
            DefaultMutableTreeNode contactName;
    
            // dummies
            List<Contact> contacts = new ArrayList<Contact>();
            contacts.add(new Contact("Me", true));
            contacts.add(new Contact("You"));
    
            Iterator<Contact> contactItr = contacts.iterator();
            while(contactItr.hasNext()){
                contactName = new DefaultMutableTreeNode(contactItr.next());
                top.add(contactName);
            }
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    Browser browsr = new Browser();
                }
            });
    
        }
    
        // dummy
        private static class Contact {
    
            private boolean someProperty;
            private String name;
    
            public Contact(String name) {
                this(name, false);
            }
    
            public Contact(String name, boolean property) {
                this.someProperty = property;
                this.name = name;
            }
    
            public boolean isSomeProperty() {
                return someProperty;
            }
    
            public String getName() {
                return name;
            }
    
            @Override
            public String toString() {
                return name;
            }
        }
    
        // this is what you want
        private static class MyTreeCellRenderer extends DefaultTreeCellRenderer {
    
            @Override
            public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
                super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
    
                // decide what icons you want by examining the node
                if (value instanceof DefaultMutableTreeNode) {
                    DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
                    if (node.getUserObject() instanceof String) {
                        // your root node, since you just put a String as a user obj                    
                        setIcon(UIManager.getIcon("FileView.computerIcon"));
                    } else if (node.getUserObject() instanceof Contact) {
                        // decide based on some property of your Contact obj
                        Contact contact = (Contact)  node.getUserObject();
                        if (contact.isSomeProperty()) {
                            setIcon(UIManager.getIcon("FileView.hardDriveIcon"));
                        } else {
                            setIcon(UIManager.getIcon("FileChooser.homeFolderIcon"));
                        }
                    }
                }
    
                return this;
            }
    
        }
    }
    

    This should get you started. You should read more about this in a JTree tutorial.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This could be a duplicate question, but I have no idea what search terms
I have been unable to fix a problem with Java Unicode and encoding. The
I have thousands of HTML files to process using Groovy/Java and I need to
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.