【问题标题】:How to fill or reload a jtree in java如何在java中填充或重新加载jtree
【发布时间】:2018-02-04 00:12:07
【问题描述】:

我在一帧中有 2 个 JTree,例如,如果我选择蓝色(从左侧树),它将填充右侧的树,之后,如果我现在选择红色,蓝色显示右侧不会消失以显示红色。

我尝试了三种不同的方法,例如 reloading()、repainting()、设置为 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);
        }
    }

【问题讨论】:

标签: java swing jtree


【解决方案1】:

我当然可能是错的,但听起来你想要的是某种类型的 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 在 cmets 中的问题:“如何删除所有节点 包括根?”

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

【讨论】:

  • @user2770352 - 请参阅上面帖子中的编辑以回答您在评论中提供的问题。
猜你喜欢
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 2010-10-19
  • 1970-01-01
  • 2012-04-18
  • 1970-01-01
  • 2012-06-19
  • 1970-01-01
相关资源
最近更新 更多