使JTree叶子显示为空目录而不是文件

时间:2012-12-06 15:26:10

标签: java swing jtree

我正在学习Java JTree的方法,但我有一个我无法弄清楚的小问题。 使用我在Oracle网站上找到的教程,我模仿了他们演示的树形结构。问题是,我希望文件夹“网站”是一个空文件夹,但JTree显示它就好像它是一个文件。我怎么能告诉JTree“网站”实际上是一个空文件夹?

更新

我开始研究一个简单的'联系经理'。我想要做的基本上它使名称看起来像文件夹(因为我想向每个人添加信息),但没有添加东西。即有些人可能没有任何信息,但我仍然希望他们看起来像文件夹。

enter image description here

Browser类的代码:

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

}

2 个答案:

答案 0 :(得分:8)

这就是您创建自己的TreeCellRenderer的方法,它会告诉您JTree您想要的任何内容。由于您使用带有DefaultMutableTreeNode的默认树模型作为节点,因此您必须提取其用户对象并根据该对象决定要绘制的内容。请注意,默认渲染器只是一个扩展JLabel,这就是您可以在其中使用setIcon(...)setText(...)等的原因。

您的叶节点涂有文件图标的原因可能是默认渲染器基于DefaultMutableTreeNode.getAllowsChildren()选择图标,类似于我使用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;
        }

    }
}

这应该让你开始。您应该在JTree tutorial中了解更多相关信息。

答案 1 :(得分:8)

配置treeModel以查询其节点的askAllowsChildren属性,以决定是否应将其视为文件夹(即使没有子节点)

DefaultMutableTreeNode root = new DefaultMutableTreeNode("I'm the root");
// node allowsChildren - that's the default 
root.add(new DefaultMutableTreeNode("folder always"));
// node doesn't accept children
root.add(new DefaultMutableTreeNode("folder never", false));
// model configured to query the node's allowsChildren property
DefaultTreeModel model = new DefaultTreeModel(root, true);