Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 165437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:55:15+00:00 2026-05-11T11:55:15+00:00

I have a JTree implementation for directories in the file system. The problem I

  • 0

I have a JTree implementation for directories in the file system. The problem I have is that when a directory is removed or renamed, the tree still shows the directory in it’s parent. The parent tree seems not to be updated for any changes once a node has been expanded.

The model I have written does not cache (I have commented out what limited caching it did have) – it’s like the JTree itself has cached the node.

The code (the model is a nested subclass):

public class FileSystemTree extends JTree {  // ***************************************************************************** // INSTANCE CREATE/DELETE // *****************************************************************************  public FileSystemTree() {     this(new Model(null,null,null));     }  public FileSystemTree(String startPath) {     this(new Model(startPath,null,null));     }  public FileSystemTree(FileSelector inc, FileSelector exc) {     this(new Model(null,inc,exc));     }  public FileSystemTree(String startPath, FileSelector inc, FileSelector exc) {     this(new Model(startPath,inc,exc));     }  private FileSystemTree(Model model) {     super(model);      //setLargeModel(true);     setRootVisible(false);     setShowsRootHandles(true);     putClientProperty('JTree.lineStyle','Angled');     }  // ***************************************************************************** // INSTANCE METHODS - ACCESSORS // *****************************************************************************  public Object getRoot() {     return getModel().getRoot();     }  // ***************************************************************************** // INSTANCE METHODS // *****************************************************************************  public String convertValueToText(Object value,boolean selected,boolean expanded,boolean leaf,int row,boolean hasFocus) {     File                                fil=(File)value;      return (fil.getName().length()!=0 ? fil.getName() : fil.getPath());     }  // ***************************************************************************** // STATIC NESTED CLASSES - SUPPORTING MODEL // *****************************************************************************      static class Model     extends Object     implements TreeModel, FilenameFilter     {      private File                        root;                                   // tree root     //ivate Map                         cache;                                  // caches child counts for directories     private File[]                      fsRoots;                                // copy of file system roots     private FileSelector                include;                                // inclusion selector     private FileSelector                exclude;                                // exclusion selector     private java.util.List              listeners=new ArrayList();      public Model(String roo, FileSelector inc, FileSelector exc) {         super();          root=(roo==null ? DRIVES : new File(roo));         //cache=new HashMap();         fsRoots=(root==DRIVES ? rootList() : null);         include=inc;         exclude=exc;         }      // *****************************************************************************     // METHODS - MODEL     // *****************************************************************************      public Object getRoot() {         return root;         }      public Object getChild(Object parent, int index) {         File                            dir=(File)parent;          if(dir==DRIVES) {             File[]   chl=fsRoots; //rootList();             return (index<chl.length ? chl[index] : null);             }         else {             String[] chl=dirList(dir);             return (index<chl.length ? new File(dir,chl[index]) : null);             }         }      public int getChildCount(Object parent) {         File                            dir=(File)parent;         //Integer                         cch=(Integer)cache.get(dir);          //if(cch!=null) {         //    return cch.intValue();         //    }          if(dir==DRIVES) {             return fsRoots.length; //rootList().length;             }         else if(dir.isDirectory()) {             return dirList(dir).length;             }         else {             return 0;             }         }      public boolean isLeaf(Object node) {         return((File)node).isFile();         }      public void valueForPathChanged(TreePath path, Object newValue) {         }      public int getIndexOfChild(Object parent, Object child) {         File                            dir=(File)parent;         File                            fse=(File)child;          if(dir==DRIVES) {             File[] ca=fsRoots; //rootList();             for(int xa=0; xa<ca.length; xa++) {                 if(fse.equals(ca[xa])) { return xa; }                 }             }         else {             String[] ca=dirList(dir);             for(int xa=0; xa<ca.length; ++xa) {                 if(fse.getName().equals(ca[xa])) { return xa; }                 }             }         return -1;         }       private File[] rootList() {         File[]                          lst=File.listRoots();          if(lst==null) { lst=new File[0]; }         //cache.put(DRIVES,new Integer(lst.length));         return lst;         }       private String[] dirList(File dir) {         String[]                        lst=dir.list(this);          if(lst==null) { lst=new String[0]; }         //cache.put(dir,new Integer(lst.length));         return lst;         }      // *****************************************************************************     // METHODS - FILENAME FILTER     // *****************************************************************************      public boolean accept(File dir, String nam)  {         return ((include==null || include.accept(dir,nam)) && (exclude==null || !exclude.accept(dir,nam)));         }      // *****************************************************************************     // METHODS - LISTENER     // *****************************************************************************      public void addTreeModelListener(TreeModelListener listener) {         if(listener != null && !listeners.contains(listener)) {             listeners.add(listener);             }         }      public void removeTreeModelListener(TreeModelListener listener) {         if(listener != null) {             listeners.remove(listener);             }         }      public void fireTreeNodesChanged(TreeModelEvent evt) {         Iterator                        itr=listeners.iterator();          while(itr.hasNext()) {             TreeModelListener listener=(TreeModelListener)itr.next();             listener.treeNodesChanged(evt);             }         }      public void fireTreeNodesInserted(TreeModelEvent evt) {         Iterator                        itr=listeners.iterator();          while(itr.hasNext()) {             TreeModelListener listener=(TreeModelListener)itr.next();             listener.treeNodesInserted(evt);             }         }      public void fireTreeNodesRemoved(TreeModelEvent evt) {         Iterator                        itr=listeners.iterator();          while(itr.hasNext()) {             TreeModelListener listener=(TreeModelListener)itr.next();             listener.treeNodesRemoved(evt);             }         }      public void fireTreeStructureChanged(TreeModelEvent evt) {         Iterator                        itr=listeners.iterator();          while(itr.hasNext()) {             TreeModelListener listener=(TreeModelListener)itr.next();             listener.treeStructureChanged(evt);             }         }     } // END INNER CLASS  // ***************************************************************************** // STATIC PROPERTIES // *****************************************************************************  static private final File               DRIVES=new File('*DRIVES');             // marker for listing file system drives  } // END PUBLIC CLASS 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T11:55:16+00:00Added an answer on May 11, 2026 at 11:55 am

    The model is responsible for notifying the listeners of any modification in the tree structure. The tree will not refresh itself in absence of such a notification.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 103k
  • Answers 103k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I think the issue has to do with how WPF… May 11, 2026 at 8:24 pm
  • Editorial Team
    Editorial Team added an answer $("div:has(p)").addClass("has-paragraph"); will add a class to all divs that contain… May 11, 2026 at 8:24 pm
  • Editorial Team
    Editorial Team added an answer There is no simple method for this for sure. Edit:… May 11, 2026 at 8:24 pm

Related Questions

I have a JTree with multiple roots (Of course, I actually have an invisible
I have a JTree that implements multi selection logic. This works great when I
I have a dialog where each entry in a JTree has its corresponding options
This question is about a Java JTree or a Window .Net Tree (Winforms) or

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.