I am trying to create a class that is basically a treeitem having the file structure of computer (yes, I am trying to create a file manager here). I have set the TreeView to lazy load, and it works well. But it takes sometime with really long directories (directories that have a lot of folders, such as %Windir%/WinSxS
I have implemented the progress indicator (the work isn't final yet, but this is how I would want). It works too, but it doesn't update the ui.
I know that the task updating progress indicator has to run in another thread (other than javafx thread). So I have tried Thread
, Platform#runLater
, CountdownLatch
etc (it Is possible that I might have implemented it the wrong way). But I am either unable to update the progress indicator ui or run into Concurrent Modification
error. Below is my code so far of the class (with one of my many attempts):
class FileTreeItemImpl extends TreeItem<String> {
private boolean isLeaf = false;
private boolean childrenCached = false;
private boolean leafCached = false;
// File is basically one of root dirs
private final File file;
// pi is passed as constructor from the controller class (in which pi is injected via FXML)
private volatile ProgressIndicator pi;
// This is to control pi's progress
private static volatile double x = 0;
public FileTreeItemImpl() {
this("Root", new File("/"), null);
}
public FileTreeItemImpl(String file, File fileObj, ProgressIndicator pi) {
super(file);
this.pi = pi;
this.file = fileObj;
}
@Override
public ObservableList<TreeItem<String>> getChildren() {
if (!childrenCached) {
childrenCached = true;
if (file != null && file.isDirectory()) {
File[] files = file.listFiles();
if (files != null) {
x = 0;
new Thread() {
@Override
public void run() {
for (File childFile : files) {
pi.setProgress(x / (files.length - 1));
x++;
if (childFile.isDirectory())
getChildren().add(new FileTreeItemImpl(childFile.getName(), childFile, pi));
}
}
}.start();
}
}
}
return super.getChildren();
}
@Override
public boolean isLeaf() {
if (!leafCached) {
leafCached = true;
isLeaf = file.isFile();
}
return isLeaf;
}
}
Note: The above code does exactly what I want but until the number of child folders of that directory are low. If there are too many (again, like in %winddir%/WinSxS
), then I get the ConcurrentModificationException
.
EDIT: In any case, here is the stacktrace:
Exception in thread "JavaFX Application Thread" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:386)
at java.util.AbstractList$Itr.next(AbstractList.java:355)
at javafx.scene.control.TreeItem.updateExpandedDescendentCount(TreeItem.java:892)
at javafx.scene.control.TreeItem.getExpandedDescendentCount(TreeItem.java:880)
at javafx.scene.control.TreeItem.updateExpandedDescendentCount(TreeItem.java:894)
at javafx.scene.control.TreeItem.getExpandedDescendentCount(TreeItem.java:880)
at javafx.scene.control.TreeItem.updateExpandedDescendentCount(TreeItem.java:894)
at javafx.scene.control.TreeItem.getExpandedDescendentCount(TreeItem.java:880)
at javafx.scene.control.TreeItem.updateExpandedDescendentCount(TreeItem.java:894)
at javafx.scene.control.TreeItem.getExpandedDescendentCount(TreeItem.java:880)
at javafx.scene.control.TreeUtil.getExpandedDescendantCount(TreeUtil.java:40)
at javafx.scene.control.TreeUtil.updateExpandedItemCount(TreeUtil.java:49)
at javafx.scene.control.TreeView.updateExpandedItemCount(TreeView.java:1042)
at javafx.scene.control.TreeView.getExpandedItemCount(TreeView.java:614)
at javafx.scene.control.TreeView$TreeViewFocusModel.getItemCount(TreeView.java:1649)
at javafx.scene.control.FocusModel.isFocused(FocusModel.java:128)
at javafx.scene.control.TreeCell.updateFocus(TreeCell.java:557)
at javafx.scene.control.TreeCell.indexChanged(TreeCell.java:476)
at javafx.scene.control.IndexedCell.updateIndex(IndexedCell.java:116)
at com.sun.javafx.scene.control.skin.VirtualFlow.setCellIndex(VirtualFlow.java:1957)
at com.sun.javafx.scene.control.skin.VirtualFlow.addLeadingCells(VirtualFlow.java:1246)
at com.sun.javafx.scene.control.skin.VirtualFlow.layoutChildren(VirtualFlow.java:1194)
at javafx.scene.Parent.layout(Parent.java:1079)
at javafx.scene.Parent.layout(Parent.java:1085)
at javafx.scene.Parent.layout(Parent.java:1085)
at javafx.scene.Parent.layout(Parent.java:1085)
at javafx.scene.Scene.doLayoutPass(Scene.java:552)
at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2397)
at com.sun.javafx.tk.Toolkit.lambda$runPulse$30(Toolkit.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:354)
at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:381)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:510)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:490)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$404(QuantumToolkit.java:319)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
I had a similar implementation of using a ProgressIndicator
as a part of a TreeItem
. - I solved it by using Task
which i use to update the UI.
my Task
implementation looks like this:
class ProgressWorker extends Task<Void> {
...
@Override
protected Void call() throws Exception {
while (getCurrentRow() < getMaxRow()) {
Thread.sleep(100);
updateProgress(getCurrentRow(), getMaxRow());
updateMessage(getTextMessage());
}
return null;
}
}
Use bind
to bind the Tasks progressProperty to the Indicator:
progressIndicator.progressProperty().bind(this.worker.progressProperty());
Now start the worker as Thread. - And your operation as thread. inside of your thread, you should be able to update Task which its property is bound to the indicator.
(pretty depressing, its harder than I thought to explain this in english)