3
 public class TabularMibTreeModel extends DefaultTreeModel {
     public TabularMibTreeModel() {
         super(new DefaultMutableTreeNode("Tablular MIBs"));
         loadParentMibs();
     }

     @Override
     public void reload() {
         super.reload();
     }

     private void loadParentMibs() {
           try {
                 ResultSet rs=Application.getDBHandler().executeQuery(SQLQueries.SELECT_TABULARMIBS_PARENT);

               while(rs.next()){            
                    String entryOID = rs.getString("mt_oid");       
                    DefaultMutableTreeNode node=new DefaultMutableTreeNode(new MibNode(entryOID, "Parent"));
                    node.add(new DefaultMutableTreeNode(Boolean.TRUE));

                    ((DefaultMutableTreeNode) getRoot()).insert(node,((DefaultMutableTreeNode)getRoot()).getChildCount())

                     node=null;

                 }

          } catch (SQLException e) {
           e.printStackTrace();
         }
     }

 }

我刚开始研究JTree。我创建了一个以 entryOID 作为父级的 jtree。实际上在后端 entryOID 有它的父级。我能得到这个参数吗?如何通过避免 entryOID 来显示它?请帮忙?

4

1 回答 1

1
 private void loadParentMibs throws SQLException () {

   ResultSet resultSet = Application.getDBHandler()
     .executeQuery(SQLQueries.SELECT_TABULARMIBS_PARENT);

   DefaultMutableTreeNode root = (DefaultMutableTreeNode)getRoot();

   Map<String, DefaultMutableTreeNode> parentsMap
     = new HashMap<String, DefaultMutableTreeNode>();

   while( resultSet.next() ) {  
     String parentId = resultSet.getString("mt_oid");

     DefaultMutableTreeNode parent = parentsMap.get(parentId);
     if ( parent == null ) {
       parent = new DefaultMutableTreeNode( parentId );
       parentsMap.add( parentId, parent );
       root.add( parent );
     }

       // Are you sure, you want Boolean.TRUE leafs 
       //   in number of parent coincindence?
       // May be you want leafs to contains some 
       //   additional info from resultSet entity?
     DefaultMutableTreeNode leaf = new DefaultMutableTreeNode(Boolean.TRUE);
     parent.add(leaf);
   }

 }
于 2012-11-16T16:11:52.770 回答