简体   繁体   中英

JTree from ArrayList not displaying nodes

I have a custom data structure that is basically just a named ArrayList of ArrayLists. Similar to XML.

(have stripped out un-nesecary code)

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


        public Element(String n){

            name = n;
        }

        @Override
        public String toString(){

            return name;
        }
    }

I am trying to display this in a JTree using a custom TreeModel class. However the JTree does not display properley. Only one node is displayed at the end of a branch, when the child node is selected it shows the last child node but when un-selected it shows the first child node, but still at the end of the branch.

捕获

From extensive de-bugging I can see it reads the all the child nodes and count correctly it just doens't display them. I suspect they are all being displayed on top of each other but don't know why or what to do about it.

Any thoughs appreciated.

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

The problem is the way in which you are using the Element class and extending it from ArrayList . This comes down to how the hashcode is calculated from an ArrayList (or AbstractList to be more accurate.

The hashcode is calculted based on the elements in the ArrayList , which is 0 for all the child Element s, resulting in a hashcode of 1 for all of them, which is causing issues with the List look up and uniquely identifying the elements.

Personally, I would create a Node class which contained a List member and which provided additional functionality that could work with the TreeModel or just use TreeNode ...

For example...

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

Or you could just use one of the pre-defined TreeNode classes, like DefaultMutableTreeNode

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM