gpt4 book ai didi

java - 如何在jtree中显示数据

转载 作者:行者123 更新时间:2023-12-01 09:58:27 24 4
gpt4 key购买 nike

我正在尝试从数据库中获取数据并在 jtree 中显示。我遇到问题 ResultSet 在我的代码下面显示最后一个值数据

rs1=DB.Select("SELECT AcCode from tblCoa order by AcCode");

while(rs1.next()){
DefaultMutableTreeNode Catagory=new DefaultMutableTreeNode(rs1.getString("AcCode"));
DefaultMutableTreeNode Catagories=new DefaultMutableTreeNode(rs1.getString("AcCode"));
DefaultMutableTreeNode Catagories2=new DefaultMutableTreeNode(rs1.getString("AcCode"));
DefaultMutableTreeNode Catagories3=new DefaultMutableTreeNode(rs1.getString("AcCode"));
DefaultMutableTreeNode Catagories4=new DefaultMutableTreeNode(rs1.getString("AcCode"));
Catagory.add(Catagories);
Catagories.add(Catagories2);
Catagories.add(Catagories3);
Catagories.add(Catagories4);
DefaultTreeModel dtm=new DefaultTreeModel(Catagory);
this.jTree1.setModel( dtm);
}

最佳答案

  DefaultMutableTreeNode Catagory = new DefaultMutableTreeNode();


while(rs1.next()){
Catagory.add(rs1.getString("AcCode"));
}
DefaultTreeModel dtm=new DefaultTreeModel(Catagory);
this.jTree1.setModel( dtm);

您在 while 循环内初始化变量。这将在每个循环中重置以前的数据。并且您有多个 DefaultMutableTreeNode 变量,它们将存储相同的 rs1.getString("AcCode") 。我不明白为什么。对代码做了一些更改,现在尝试一下。

关于java - 如何在jtree中显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36998868/

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