来自ArrayList的JTree不显示节点

时间:2014-03-11 06:17:32

标签: java swing jtree

我有一个自定义数据结构,它基本上只是ArrayLists的一个命名ArrayList。与XML类似。

(已经删除了非必要的代码)

    public class Element extends ArrayList<Element> {
        private String name;


        public Element(String n){

            name = n;
        }

        @Override
        public String toString(){

            return name;
        }
    }

我试图使用自定义TreeModel类在JTree中显示它。然而,JTree没有显示正确的。在分支的末尾只显示一个节点,当选择子节点时,它显示最后一个子节点但未选中时显示第一个子节点,但仍然在分支的末尾。

capture

从广泛的调试中我可以看到它读取所有子节点并正确计数它只是不显示它们。我怀疑他们都是在彼此之上展示,但不知道为什么或该做些什么。

任何人都赞赏。

public class TestModel implements TreeModel{

    Element data;

    TestModel(){      
        data = new Element("data");

        data.add(new Element("One"));
        data.add(new Element("Two"));
        data.add(new Element("Three"));
        data.add(new Element("Four"));
        data.add(new Element("Five"));
    }

    @Override
    public Object getRoot() {
        return data;
    }

    @Override
    public Object getChild(Object parent, int index) {

        if(parent instanceof Element){
            Element p = (Element)parent;
            Element child = p.get(index);

            return child;
        }

        return null;
    }

    @Override
    public int getChildCount(Object parent) {

        if(parent instanceof Element){
            Element e = (Element)parent;
            return e.size();
        }

        return 0;
    }

    @Override
    public int getIndexOfChild(Object parent, Object child) {

        if(parent instanceof Element){
            Element e = (Element)parent;

            return e.indexOf(child);
        }

        return -1;

    }

    @Override
    public boolean isLeaf(Object node) {
        //List<? super ArrayList> d = (List<? super ArrayList>) node;

        if(node instanceof Element){
            Element e = (Element)node;
            return e.isEmpty();
        }

        return true;
    }
}

1 个答案:

答案 0 :(得分:1)

问题在于您使用Element类并从ArrayList扩展它的方式。这取决于hashcode如何从ArrayList(或AbstractList计算得更准确。

hashcode根据ArrayList中的元素进行计算,0为所有子Element的{​​{1}},结果为hashcode所有这些都1,这会导致List查找问题并唯一识别元素。

就个人而言,我会创建一个Node类,其中包含List成员,并提供了可以与TreeModel一起使用或只使用TreeNode的其他功能...

例如......

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

public class TestTree {

    public static void main(String[] args) {
        new TestTree();
    }

    public TestTree() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JTree tree = new JTree(new TestModel());

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new JScrollPane(tree));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public static class TestModel implements TreeModel {

        Element data;

        TestModel() {
            data = new Element("data");

            data.add(new Element("One"));
            data.add(new Element("Two"));
            data.add(new Element("Three"));
            data.add(new Element("Four"));
            data.add(new Element("Five"));
        }

        @Override
        public Object getRoot() {
            return data;
        }

        @Override
        public Object getChild(Object parent, int index) {

            System.out.println("GetChild from " + parent + " @ " + index);

            if (parent instanceof Element) {
                Element p = (Element) parent;
                Object child = p.getChildAt(index);

                System.out.println("child = " + child);
                return child;
            }

            return null;
        }

        @Override
        public int getChildCount(Object parent) {

            if (parent instanceof Element) {
                Element e = (Element) parent;
                System.out.println("childCount = " + parent + "; " + e.getChildCount());
                return e.getChildCount();
            }

            return 0;
        }

        @Override
        public int getIndexOfChild(Object parent, Object child) {

            if (parent instanceof Element && child instanceof Element) {
                Element e = (Element) parent;

                System.out.println("indexOf " + child + " in " + parent + " is " + e.getIndex((Element)child));
                return e.getIndex((Element)child);
            }

            return -1;

        }

        @Override
        public boolean isLeaf(Object node) {
            //List<? super ArrayList> d = (List<? super ArrayList>) node;

            if (node instanceof Element) {
                Element e = (Element) node;
                System.out.println("isLeaf " + e + "; " + (e.getChildCount() == 0));
                return e.getChildCount() == 0;
            }

            return true;
        }

        @Override
        public void valueForPathChanged(TreePath path, Object newValue) {

        }

        @Override
        public void addTreeModelListener(TreeModelListener l) {

        }

        @Override
        public void removeTreeModelListener(TreeModelListener l) {

        }
    }

    public static class Element implements TreeNode {

        private List<Element> nodes;
        private Element parent;

        private String name;

        public Element(String n) {

            nodes = new ArrayList<>(25);
            name = n;
        }

        @Override
        public String toString() {

            return name;
        }

        protected void setParent(Element parent) {
            this.parent = parent;
        }

        public void add(Element node) {
            node.setParent(this);
            nodes.add(node);
        }

        public void remove(Element node) {
            node.setParent(null);
            nodes.remove(node);
        }

        @Override
        public TreeNode getChildAt(int childIndex) {
            return nodes.get(childIndex);
        }

        @Override
        public int getChildCount() {
            return nodes.size();
        }

        @Override
        public TreeNode getParent() {
            return parent;
        }

        @Override
        public int getIndex(TreeNode node) {
            return nodes.indexOf(node);
        }

        @Override
        public boolean getAllowsChildren() {
            return true;
        }

        @Override
        public boolean isLeaf() {
            return nodes.isEmpty();
        }

        @Override
        public Enumeration children() {
            return Collections.enumeration(nodes);
        }
    }
}

或者您可以使用其中一个预定义的TreeNode类,例如DefaultMutableTreeNode