gpt4 book ai didi

java - ArrayList 中的 JTree 不显示节点

转载 作者:行者123 更新时间:2023-12-01 13:17:55 26 4
gpt4 key购买 nike

我有一个自定义数据结构,它基本上只是一个名为 ArrayList 的 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;
}
}

最佳答案

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

hashcode是根据ArrayList中的元素计算的,对于所有子Element都是0 >s,导致所有元素的 hashcode1,这导致 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

关于java - ArrayList 中的 JTree 不显示节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22317870/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com