简体   繁体   中英

JTree, cant add nodes

In the code bellow at the line specified the node adding dosent work,while the above syso line prints the tables as normal.If I put the tables.add(new ...("blabla") bellow the category.add(triggers); line it will work.

DefaultMutableTreeNode tables,procedures,functions,triggers;
public void loadConnections(){
    for(Database db:SavedData.databases){
        db.connect();
        DefaultMutableTreeNode category = new DefaultMutableTreeNode(db.name);
        ((DefaultMutableTreeNode)(((DefaultTreeModel)treeConnections.getModel()).getRoot())).add(category);
        tables = new DefaultMutableTreeNode("Tables");
        procedures = new DefaultMutableTreeNode("Procedures");
        functions = new DefaultMutableTreeNode("Functions");
        triggers = new DefaultMutableTreeNode("Triggers");

        category.add(tables);
        category.add(functions);   
        category.add(procedures);
        category.add(triggers);  
    }

    treeConnections.addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent me) {
        TreePath tp = treeConnections.getPathForLocation(me.getX(), me.getY());
        if (tp != null){
            if(tp.getPathCount()==3){
                String dbname=tp.getParentPath().getLastPathComponent().toString();
                String function=tp.getLastPathComponent().toString();
                Database db=SavedData.getConnection(dbname);
                if(function.equals("Tables")){
                    new Thread(new Runnable() {
                        public void run() {
                            try{
                                ArrayList<String> tablesList=db.connectionInterface.getTables();
                                tables.removeAllChildren();
                                for(String table:tablesList){
                                    System.out.println(table);

                                    tables.add(new DefaultMutableTreeNode(table));  ->>>>>>>>>>>>>>>here
                                }
                            }catch(Exception ex){ex.printStackTrace();}
                        }
                    }).start();    
                }
            }
        }  
      }
    });
}

As copeg said I tried the DefaultTreeModel.inserNodeInto method plus some other changes and it worked

DefaultMutableTreeNode tables,procedures,functions,triggers;
public void loadConnections(){
    for(Database db:SavedData.databases){
        db.connect();
        DefaultMutableTreeNode category = new DefaultMutableTreeNode(db.name);
        ((DefaultMutableTreeNode)(((DefaultTreeModel)treeConnections.getModel()).getRoot())).add(category);
        tables = new DefaultMutableTreeNode("Tables");
        procedures = new DefaultMutableTreeNode("Procedures");
        functions = new DefaultMutableTreeNode("Functions");
        triggers = new DefaultMutableTreeNode("Triggers");

        category.add(tables);
        category.add(functions);   
        category.add(procedures);
        category.add(triggers);  

        ((DefaultTreeModel)treeConnections.getModel()).insertNodeInto(new DefaultMutableTreeNode("fyh"), tables, 0);
    }


    treeConnections.addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent me) {
        TreePath tp = treeConnections.getPathForLocation(me.getX(), me.getY());
        if (tp != null){
            if(tp.getPathCount()==3){
                String dbname=tp.getParentPath().getLastPathComponent().toString();
                String function=tp.getLastPathComponent().toString();
                Database db=SavedData.getConnection(dbname);
                if(function.equals("Tables")){
                   new Thread(new Runnable() {
                        public void run() {
                            try{
                                ArrayList<String> tablesList=db.connectionInterface.getTables();
                                DefaultTreeModel model = (DefaultTreeModel) treeConnections.getModel();
                                TreePath path = treeConnections.getNextMatch("Tables", 0, Position.Bias.Forward);
                                MutableTreeNode node = (MutableTreeNode) path.getLastPathComponent();
                                node.remove(0);
                                while(node.getChildCount()>0){
                                    node.remove(0);
                                }
                                model.reload();
                                int index=0;
                                for(String table:tablesList){
                                    MutableTreeNode newNode = new DefaultMutableTreeNode(table);
                                    model.insertNodeInto(newNode, node,index++);
                                    treeConnections.expandPath(path);   
                                }
                            }catch(Exception ex){ex.printStackTrace();}
                        }
                    }).start();    
                }
            }
        }  
      }
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM