gpt4 book ai didi

java - JTree 延迟加载 展开

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:01:24 25 4
gpt4 key购买 nike

希望你一切都好。

我有一个 JTree,我使用数据库查询(每个节点)延迟加载它。我在屏幕上有一个按钮,当用户单击该按钮时,我在数据库中执行一些 insert/update/delete 然后将模型添加回 Jtree 只有 root 及其使用 SQL 的直接子级(这会折叠树和选择,并且所有展开的节点都会折叠,这是预期的,因为它是延迟加载的树)

我想要完成的是在用户点击insert/update/delete按钮之前,我读取所有展开状态并将其保存在内存中,然后当我重新初始化JTree 我遍历那些展开的子节点并调用 jtree.expandPath 但它不会展开节点 :(。

我什至添加了一个 TreeWillExpandListener 并在 treeWillExpand 事件中添加了代码以从数据库中获取节点的子节点,我看到确实执行了查询以获取子节点但是 JTree 仍然显示为折​​叠状态。

我真的需要你的帮助。请指教。这是代码片段。

package marketdatagui;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import javax.swing.event.TreeModelListener;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.EventListenerList;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.io.File;
import java.io.Serializable;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;


public class StressGroupModel
implements TreeModel,Serializable, Cloneable
{
protected EventListenerList listeners;

private static final Object LEAF = new Serializable() { };

private Map map;

private NodeData root;

private class NodeData{
String id;
String name;

public String toString(){
return name;
}
}

public StressGroupModel()
{
this.root = new NodeData();
root.id = "Root";
root.name = "Root";

this.listeners = new EventListenerList();

this.map = new HashMap();

List rootChildren = new ArrayList();

NodeData d1 = new NodeData();
d1.id = "5000007";
d1.name = "Name5000007";

rootChildren.add(d1);

NodeData d2 = new NodeData();
d2.id = "10000054";
d2.name = "Name10000054";

rootChildren.add(d2);
map.put(root.id, rootChildren);
}


public Object getRoot()
{
return root;
}

public boolean isLeaf(Object node)
{
return map.get(node) == LEAF;
}

public int getChildCount(Object node)
{
List children = children(node);

if (children == null)
return 0;

return children.size();
}

public Object getChild(Object parent, int index)
{
return children(parent).get(index);
}

public int getIndexOfChild(Object parent, Object child)
{
return children(parent).indexOf(child);
}

protected List children(Object node)
{
NodeData s = (NodeData)node;

Object value = map.get(s.id);

if (value == LEAF)
return null;

List children = (List)value;

if (!"Root".equals(s.id))
{
String[][] dbData = getChildren(s.id);

if (dbData != null)
{
children = new ArrayList(dbData.length);
for (int i = 0; i < dbData.length; i++)
{
NodeData d = new NodeData();
d.id = dbData[i][1];
d.name = dbData[i][1] + "Name";
children.add(d);
if ("R".equals(dbData[i][2]))
map.put(d, LEAF);
}
}
else
children = new ArrayList(0);

map.put(s.id, children);
}

return children;
}

private String[][] getChildren(String parent_uid){
String sql = "select parent_uid,child_uid,child_type from stress_groups_mapping "
+ " where parent_uid="+ parent_uid
+" order by parent_uid, child_uid";

return Util.getTableData(sql);
}

public void valueForPathChanged(TreePath path, Object value)
{
}

public void addTreeModelListener(TreeModelListener l)
{
listeners.add(TreeModelListener.class, l);
}

public void removeTreeModelListener(TreeModelListener l)
{
listeners.remove(TreeModelListener.class, l);
}

public Object clone()
{
try
{
StressGroupModel clone = (StressGroupModel)super.clone();

clone.listeners = new EventListenerList();

clone.map = new HashMap(map);

return clone;
}
catch (CloneNotSupportedException e)
{
throw new InternalError();
}
}

}
  • 这是我创建树的方法

public createTree{

JFrame f = new JFrame("Tree Dragging Tester");
DefaultMutableTreeNode root = new DefaultMutableTreeNode(new Object());
jtree = new JTree(new StressGroupModel());
TreeWillExpandListener treeWillExpandListener = new TreeWillExpandListener() {
public void treeWillCollapse(TreeExpansionEvent treeExpansionEvent)
throws ExpandVetoException {


}

public void treeWillExpand(TreeExpansionEvent treeExpansionEvent) throws ExpandVetoException {
TreePath path = treeExpansionEvent.getPath();
Object node = path.getLastPathComponent();
((StressGroupModel)jtree.getModel()).getChildCount(node);
}


};

jtree.addTreeWillExpandListener(treeWillExpandListener);

f.getContentPane().add(jtree, BorderLayout.CENTER);
f.setSize(300, 200);
f.setVisible(true);
}
  • 这是我保存展开状态的方法

Enumeration paths = jtree.getExpandedDescendants(new TreePath(jtree.getModel().getRoot()));

  • 插入/更新/删除数据库内容
  • 现在重新初始化模型并添加回 JTree(所以它会选择最新的来自数据库的值)
  • 现在我循环并调用 expand 来展开用户展开的节点在他点击按钮之前,可惜它一直处于折叠状态 :(

jtree.setModel(new StressGroupModel()); while (paths.hasMoreElements()) {

            TreePath treePath = (TreePath) paths.nextElement();

try {

jtree.expandPath(treePath);
jtree.setSelectionPath(treePath);

}
catch(Exception ex){

}

}

最佳答案

我找到了解决方案。

在我的 NodeData 类中需要一个 equals 方法和 hashCode,问题出在设置新模型之后,当我试图扩展 equals 返回 false 的路径时,因为 equals 的默认实现执行“浅比较”,即只比较引用,我必须覆盖 hashCode 和 equals 方法,并在 equals 方法中进行“深度比较”。

谢谢

关于java - JTree 延迟加载 展开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6252462/

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