How to refresh a particular directory in a Jtree

Posted on 2008-11-20
Last Modified: 2013-11-23
Based on this thread,
can anyone please say, how to refresh a particular directory alone, whenever needed.

zzynx, i have reposted this question as per your suggestion. Hope I am clear with my question.

Please provide some help.

Warm Regards,
Question by:Sreejith22
    LVL 37

    Accepted Solution

    1) On the MyTreeNode object corresponding with the particular directory (let's say it is called "nodeToRefresh") call removeAllChildren() (That's a mehtod of the DefaultMutableTreeNode class it extends)


    2) Make sure your MyNode object is up-to-date with the new situation (= the children are reconstructed)

    3) Then call


    4) call


    If needed:
    [ 5) If you don't see anything changing visually it might be needed to call


    Author Comment

    Inside myTree class, is it possible for me to get the vector of contents corresponding to a particular directory?
    LVL 37

    Expert Comment

    myTree class?
    I'm not aware of the fact that you have your own myTree class...

    Author Comment

    I named the class in which I declare and create the tree as

    My question is, I get the child vector corresponding to a particular directory with  myNode.getChildren()

    Though we add it to the child vector as
    >>children.add( new MyNode(dirEntry1.filename, dirEntry1.attributes.isDirectory()) ); , i get the output in the form [my_share, jj, my_music, my_pictures, my_video, New Folder (2), search_off.png, booodrive.png, src.rar] , which is OK for me.

    But , when I perform the below mentioned, I get the exception given.

                for(Enumeration e = myNode.getChildren().elements(); e.hasMoreElements();)
                      Object o = e.nextElement();
                      nomSelection =((SFTPv3DirectoryEntry)o).filename;


    Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: booodrive.MyNode cannot be cast to com.trilead.ssh2.SFTPv3Dir
            at booodrive.myTree.createFileVector(

    I know that what is inside that vector is a MyNode object, but how can I cast this?
    LVL 37

    Expert Comment

    >> I know that what is inside that vector is a MyNode object, but how can I cast this?
    Well, you only can get the information out of a MyNode object that you previously put in.

    I don't know how your constructChildren() method and your MyNode object currently looks like,
    but if constructChildren() still contains the line I suggested:

           children.add( MyNode(dirEntry1.filename, dirEntry1.attributes.isDirectory()) );

    you see that in your MyNode object you store the filename, so you could write:

    for(Enumeration e = myNode.getChildren().elements(); e.hasMoreElements();) {
          MyNode node = (MyNode)e.nextElement();
          nomSelection = node.getName();

    Author Comment

    ok..thanks very much, I am implementing the refresh for a directory..after that I will close this thread.
    thx, again.

    Author Comment

    I am implementing drag and drop. Everything works fine for me. But one problem occured while implementing this. The solution of which requires me to have the following detail.

    If at any time, I select a particular item in a directory(say, directory x), how can I get the children vector of that directory(directory x) from MyNode?

    I tried different methods, sorry to say that with my limited knowledge, i could not get the desired vector.

    If possible, please help.
    LVL 37

    Expert Comment

    >> how can I get the children vector of that directory(directory x) from MyNode?
    getChildren() returns the Vector containing the children (also MyNode object's again)

    Author Closing Comment

    Thanks alot zzynx, soln. was awesome

    Author Comment

    Thanks alot zzynx, solution was awesome and line by line accurate.

    LVL 37

    Expert Comment

    You're welcome.
    Thanx 4 axxepting

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
    Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now