Results 1 to 4 of 4
Thread: JTree trouble
- 07-31-2007, 02:59 PM #1
Member
- Join Date
- Jul 2007
- Posts
- 2
- Rep Power
- 0
JTree trouble
Hello! i have following TreeModel (with some modifications):
thus i create JTree to browse it and make some actions to expanded tree nodes (i.e. copy/paste/delete).Java Code: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(); } } });
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.
- 07-31-2007, 07:05 PM #2
Tell the TreeModel about the change. The tree listens to the model for changes.
You might try something like
Java Code:((DefaultTreeModel)tree.getModel()).nodesWereRemoved(...args...);
- 07-31-2007, 09:08 PM #3
Member
- Join Date
- Jul 2007
- Posts
- 2
- Rep Power
- 0
Unfortunally this line made error for me.
Looks like i cannot cast tree.getModel() to DefaultTreeModel.
or this might be some bug for me? cause i seen this way in some other applications - anyway pls try this code and tell me if it works for u/
- 07-31-2007, 11:12 PM #4
Similar Threads
-
JTree Programmatic Node Expansion and Selection Probelm
By hemanthjava in forum AWT / SwingReplies: 3Last Post: 01-16-2013, 07:23 AM -
Compile Trouble
By adelgado0723 in forum New To JavaReplies: 5Last Post: 04-21-2008, 02:02 AM -
trouble with program
By jimJohnson in forum New To JavaReplies: 1Last Post: 04-03-2008, 09:29 AM -
Jtree - making parts editable
By kmarie in forum AWT / SwingReplies: 1Last Post: 07-27-2007, 02:34 AM -
how to display data in Jtree
By paty in forum New To JavaReplies: 1Last Post: 07-24-2007, 12:28 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks