如何在 java 中填充或重新加载 jtree

How to fill or reload a jtree in java

l 在一个框架中有 2 个 JTrees,碰巧,例如,如果 l select blue(从左边的树),它将填充右边的树,之后,如果 l 现在select红色,右边显示的蓝色不会因为红色显示而消失。

我已经尝试了三种不同的方法,如重新加载()、重新绘制()、设置为空,none 有效。

DefaultTreeModel defMod1 = (DefaultTreeModel)jTree1.getModel();                 
defMod1.reload();
jTree1.repaint();
defMod1.setRoot(null);

我想要的是右边的树显示左边树上当前 selection 的内容。也就是说,我如何重新加载或重新绘制右侧树以在左侧树上显示当前 selection 的内容?

我使用以下命令获取我实现的 TreeSelectionListener 接口的 valueChange() 中的当前 selection。

DefaultMutableTreeNode node = (DefaultMutableTreeNode) jTree1.getLastSelectedPathComponent();

我需要帮助。 谢谢

PS: 我试过DevilsHnd的解决方法,还是不行。部分代码如下。是的,我想要一种 clearTree() 函数。

@Override
public void valueChanged(TreeSelectionEvent e){
    JTree treeSource = (JTree) e.getSource();
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) jTree1.getLastSelectedPathComponent();//gets the selection
    if(treeSource == jTree1 ){//if it is the tree on the left
        if(node == null)//if a node is selected
    return;
        Object nodeInfo = node.getUserObject();

        if(node.isLeaf()){
        try {
            String ipp = (String)nodeInfo;
            root2 = new DefaultMutableTreeNode(InetAddress.getByName((String)nodeInfo)+ " | " + "Local Disk (C:)");//root node for the tree on the right
            selectedPcIP = InetAddress.getByName(ipp).getHostAddress();
            selectedIP = "\\" + selectedPcIP +"\c$";
            ArrayList creds = new BackItDb().getCreds();//gets user name and password from the database
            NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("",creds.get(0).toString(),creds.get(1).toString());
            try {
                SmbFile file = new SmbFile("smb://"+selectedPcIP + "/" + System.getProperty("user.home").replace('C', 'c').replace(":", "$").replaceAll("[\\]","/")+"/",auth);//gets the folders from the selected PC

                jTree1.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));                              
                createSecondTree(file,root2,selectedPcIP);//This is a function that creates the tree on the right(jtree2).

                jTree1.setCursor(Cursor.getDefaultCursor());
                treeModel2 = new DefaultTreeModel(root2);// treemodel for the tree on the right
                jTree2.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
                jTree2.setModel(treeModel2);// the tree on the right
        }catch (UnknownHostException ex) {
            Logger.getLogger(BackIt_Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

当然我可能是错的,但听起来您想要的是某种类型的 clearTree() 方法,您可以在调用填充树的方法之前调用该方法。

这可以用这种方法来完成:

/**
 * Removes all nodes from the supplied JTree except the Root.
 * 
 * @param tree (JTree) The JTree Variable Name for which to 
 * clear all nodes from.
 */
public static void clearTree(JTree tree) {
    if (tree.toString() == null) { return; }
    DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
    DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
    root.removeAllChildren();
    model.reload();
}

EDIT: As per OP's question in comments: "How to remove all nodes including the root?"

要在 post 下面的评论中回答您的问题,它实际上是一个相当简单的衬垫:yourTree_VariableName.setModel(null);。所以,你可以像这样制作另一个简单的方法:

/**
 * This method will completely wipe out (clear) all nodes 
 * from a JTree including the Root node. After using this 
 * method you will need to construct another tree model to 
 * refill it.
 * 
 * @param tree (JTree) The JTree Variable Name for which to 
 * wipe all nodes from.
 */
public static void wipeTree(JTree tree) {
    tree.setModel(null);
}