Hello! i have following TreeModel (with some modifications):
public class FileSystemModelFull implements FileSystemModel {
private File root;
private List<TreeModelListener> listeners = new ArrayList<TreeModelListener>();
public FileSystemModelFull(File f) {
root = f;
}
public File getRoot() {
return root;
}
public File getChild(Object parent, int index) {
File directory = (File) parent;
String[] children = directory.list();
return new File(directory, children[index]);
}
public int getChildCount(Object parent) {
File file = (File) parent;
if (file.isDirectory()) {
String[] fileList = file.list();
if (fileList != null)
return file.list().length;
}
return 0;
}
public boolean isLeaf (Object node) {
File file = (File) node;
return file.isFile();
}
public int getIndexOfChild (Object parent, Object child) {
File directory = (File) parent;
File file = (File) child;
String[] children = directory.list();
for (int i = 0; i < children.length; i++) {
if (file.getName().equals(children[i]))
return i;
}
return 0;
}
public void valueForPathChanged (TreePath path, Object value) {
File oldFile = (File) path.getLastPathComponent();
String fileParentPath = oldFile.getParent();
String newFileName = (String) value;
File targetFile = new File(fileParentPath, newFileName);
oldFile.renameTo(targetFile);
File parent = new File(fileParentPath);
int[] changedChildrenIndices = {getIndexOfChild(parent, targetFile)};
Object[] changedChildren = {targetFile};
fireTreeNodesChanged(path.getParentPath(), changedChildrenIndices, changedChildren);
}
private void fireTreeNodesChanged (TreePath parentPath, int[] indices, Object[] children) {
TreeModelEvent event = new TreeModelEvent(this, parentPath, indices, children);
for (TreeModelListener listener : listeners) {
listener.treeNodesChanged(event);
}
}
public void addTreeModelListener (TreeModelListener listener) {
listeners.add(listener);
}
public void removeTreeModelListener (TreeModelListener listener) {
listeners.remove(listener);
}
}
public class Main extends JFrame {
localTreeModel = new FileSystemModel(new File("c:\\")); // for win
localFileTree = new JTree(localTreeModel);
localFileTree.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
TreePath tp = localFileTree.getPathForLocation(e.getX(), e.getY());
if (e.getClickCount() == 2 && tp != null) {
File f = new File(tTreePath.getLastPathComponent().toString());
f.delete();
}
}
});
thus i create JTree to browse it and make some actions to expanded tree nodes (i.e. copy/paste/delete).
This concret example must delete single file (not directory) on double click.
After u do so u ll see that leaf identifying that file is still on place in JTree and not deleted
After this i must refresh tree - but unfortunally theres no way i can see to do so=(
Can anyone help with this situation - cause i m stucked with it for a long time already.