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:
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
}
The two issues (that I can se) are:
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 :P