DefaultMutableTreeNode

DefaultMutableTreeNode

我试图从数据库中获取数据并显示在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")。我看不出原因。对代码进行了一些更改,请立即尝试。

10-01 12:07