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 8320663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:43:58+00:00 2026-06-08T22:43:58+00:00

I’m developing an application which will work with external memories accessed through USB a

  • 0

I’m developing an application which will work with external memories accessed through USB a lot.
I’ve implemented TreeModel for browsing directories on disks. It works great for:

  • local discs on Ubuntu
  • connected external memories on Ubuntu
  • local discs on Windows 7

but it sucks for connected external memories on Windows 7 and I don’t know why. Scrolling a JTree with this model with pendrive as a root is very glitchy. At the beginning I’ve thought that listFiles() from java.io.File is slow for pendrive so I’ve added some kind of caching to model but it didn’t work – scrolling still sucks.

I’ve just noticed that it have something to do with Look&Feel. For system L&F on Windows it sucks BAD, for Nimbus L&F it is not that bad but still not perfect.

FileTreeModel:

import java.io.File;
import java.io.FileFilter;
import java.io.Serializable;
import java.util.*;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

public class FileTreeModel implements TreeModel {

    private File root;
    private boolean onlyFolders;
    private boolean showHidden;
    private final Object LEAF = new Serializable() {
    };
    private Map<File, Object> map;
    private LinkedList<TreeModelListener> listeners = new LinkedList<>();
    private FileFilter directoryFilter = new FileFilter() {

        @Override
        public boolean accept(java.io.File pathname) {
            return pathname.isDirectory();
        }
    };

    public FileTreeModel(File root, boolean onlyFolders, boolean showHidden) {
        this.root = root;
        this.onlyFolders = onlyFolders;
        this.showHidden = showHidden;
        this.map = new HashMap();
    }

    public FileTreeModel() {
    }

    public boolean isShowHidden() {
        return showHidden;
    }

    public void setShowHidden(boolean showHidden) {
        this.showHidden = showHidden;
    }

    @Override
    public Object getRoot() {
        return root;
    }

    public void setRoot(File root) {
        Object oldRoot = this.root;
        this.root = root;
        map.clear();
        TreeModelEvent event = new TreeModelEvent(root, new Object[]{root});
        for (TreeModelListener listener : listeners) {
            listener.treeStructureChanged(event);
        }
    }

    @Override
    public boolean isLeaf(Object node) {
        return ((File) node).isFile();
    }

    @Override
    public int getChildCount(Object parent) {
        if (parent instanceof java.io.File && ((java.io.File) parent).canRead()) {
            List<File> files = children(parent);
            int result = 0;
            for (java.io.File file : files) {
                if (((file.isDirectory() && onlyFolders) || !onlyFolders)
                        && ((!file.isHidden() && !showHidden) || showHidden)) {
                    result++;
                }
            }
            return result;
        }
        return 0;
    }

    @Override
    public Object getChild(Object parent, int index) {
        if (parent instanceof java.io.File) {
            List<File> files = children(parent);
            List<java.io.File> resultFiles = new LinkedList<>();
            for (java.io.File file : files) {
                if (((file.isDirectory() && onlyFolders) || !onlyFolders)
                        && ((!file.isHidden() && !showHidden) || showHidden)) {
                    resultFiles.add(file);
                }
            }
            return resultFiles.get(index);
        }
        return null;
    }

    @Override
    public int getIndexOfChild(Object parent, Object child) {
        if (parent instanceof java.io.File) {
            List<File> files = children(parent);
            List<java.io.File> resultFiles = new LinkedList<>();
            for (java.io.File file : files) {
                if (((file.isDirectory() && onlyFolders) || !onlyFolders)
                        && ((!file.isHidden() && !showHidden) || showHidden)) {
                    resultFiles.add(file);
                }
            }
            return resultFiles.indexOf(child);
        }
        return -1;
    }

    @Override
    public void valueForPathChanged(TreePath path, Object newvalue) {
    }

    @Override
    public void addTreeModelListener(TreeModelListener l) {
        listeners.add(l);
    }

    @Override
    public void removeTreeModelListener(TreeModelListener l) {
        listeners.remove(l);
    }

    //============================PRIVATE METHODS===============================
    protected List<File> children(Object node) {
        File f = (File) node;
        Object value = map.get(f);
        if (value == LEAF) {
            return null;
        }
        List children = (List) value;
        if (children == null) {
            File[] c = f.listFiles();
            if (c != null) {
                children = new ArrayList(c.length);
                for (int len = c.length, i = 0; i < len; i++) {
                    children.add(c[i]);
                    if (!c[i].isDirectory()) {
                        map.put(c[i], LEAF);
                    }
                }
            } else {
                children = new ArrayList(0);
            }
            map.put(f, children);
        }
        return children;
    }
}

Sample form:

import folderlist.model.treemodels.FileTreeModel;
import java.io.File;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;

public class NewJFrame extends javax.swing.JFrame {

    public NewJFrame() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jComboBox1 = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jButton1.setText("Refresh");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jTree1.setModel(new FileTreeModel());
        jScrollPane1.setViewportView(jTree1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jComboBox1, 0, 173, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        DefaultComboBoxModel model = new DefaultComboBoxModel(getAvailableRoots());
        jComboBox1.setModel(model);
    }

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
        File choosenRoot = (File) jComboBox1.getSelectedItem();
        if (choosenRoot != null) {
            FileTreeModel model = new FileTreeModel(choosenRoot, false, true);
            jTree1.setModel(model);
        }
    }

    public static void main(String args[]) {

        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }

    private Vector<File> getAvailableRoots() {
        Vector<File> v = new Vector<File>(10, 1);
        File userHome = new File(System.getProperty("user.home"));
        if (userHome.isDirectory()) {
            v.addElement(userHome);
        }
        File[] roots = File.listRoots();
        for (File root : roots) {
            v.addElement(root);
        }
        String os = System.getProperty("os.name").toLowerCase();
        boolean isUnix = (os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0);
        if (isUnix) {
            roots = new File("/media").listFiles();
            for (File root : roots) {
                v.addElement(root);
            }
        }
        return v;
    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTree jTree1;
    // End of variables declaration
}
  • 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. Editorial Team
    Editorial Team
    2026-06-08T22:44:00+00:00Added an answer on June 8, 2026 at 10:44 pm

    The two issues (that I can se) are:

    1. Your not caching your results. File listing is an expensive operation, it takes time. On external devices even more so. Once you’ve listed a directory, maintain the list. If you’re using Java 7, you can use the Fie Watcher Service, otherwise, you’re going to need to provide a refresh option.
    2. You should off load the loading of the nodes children tO a ackground task. Take a look at http://www.jroller.com/Thierry/entry/swing_lazy_loading_in_a & http://www.jroller.com/Thierry/entry/swing_lazy_loading_in_jtree . While not perfect, try provide a good idea

    UPDATE

    When you have a look at the code, there is a lot going on. For a individual node, probably nothing significant, but when you start getting a few nodes, the delay will start to become noticeable.

    So every time the tree needs to repaint, it’s crawling through each individual node a call getChildCount, getChildren and/or getChildAt. Each of these methods are essentially doing the same thing, over and over again.

    @Override
    public int getChildCount(Object parent) {
        if (parent instanceof java.io.File && ((java.io.File) parent).canRead()) {
            List<File> files = children(parent);
            int result = 0;
            for (java.io.File file : files) {
                if (((file.isDirectory() && onlyFolders) || !onlyFolders)
                        && ((!file.isHidden() && !showHidden) || showHidden)) {
                    result++;
                }
            }
            return result;
        }
        return 0;
    }
    
    @Override
    public Object getChild(Object parent, int index) {
        if (parent instanceof java.io.File) {
            List<File> files = children(parent);
            List<java.io.File> resultFiles = new LinkedList<>();
            for (java.io.File file : files) {
                if (((file.isDirectory() && onlyFolders) || !onlyFolders)
                        && ((!file.isHidden() && !showHidden) || showHidden)) {
                    resultFiles.add(file);
                }
            }
            return resultFiles.get(index);
        }
        return null;
    }
    

    It would be better to have a sub model that contained a list of all the File’s and then based on the properties (showHidden & onlyFolders) produce a cached sub model which could then be used to produce the results from each call.

    For example

    @Override
    public int getChildCount(Object parent) {
        int count = 0;
        if (parent instanceof java.io.File && ((java.io.File) parent).canRead()) {
            count = fileModel.getFilteredFiles().size();
        }
        return count;
    }
    

    That’s just a sugesstion.

    Also, from recent experience, File.canRead() & File.canWrite() can return false positives on Windows 7 machines where the UAC is active 😛

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I need a function that will clean a strings' special characters. I do NOT
I am trying to loop through a bunch of documents I have to put
I know there's a lot of other questions out there that deal with this

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.