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

时间:2017-08-25 22:03:09

标签: java swing jtree

enter image description here

例如,如果l选择蓝色(来自左侧树),它将填充右侧的树,之后,如果我现在选择红色,则显示蓝色显示在右边不会因为红色而显示出来。

我尝试了三种不同的方法,如reloading(),重绘(),设置为null,无效。

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

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

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

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);
        }
    }

1 个答案:

答案 0 :(得分:3)

我当然错了,但听起来你想要的是某种类型的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();
}
  

编辑:根据评论中的OP问题:“如何删除所有节点   包括根?“

要在这篇文章下面的评论中回答你的问题,它实际上是一个相当简单的一个班轮: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);
}