Results 1 to 4 of 4

Thread: JTree trouble

  1. #1
    Alantie Vala is offline Member
    Join Date
    Jul 2007
    Posts
    2
    Rep Power
    0

    Default JTree trouble

    Hello! i have following TreeModel (with some modifications):

    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();
          }
       }
    });
    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.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    10

    Default

    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...);

  3. #3
    Alantie Vala is offline Member
    Join Date
    Jul 2007
    Posts
    2
    Rep Power
    0

    Default

    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/

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    10

    Default

    It looks like FileSystemModel implements TreeModel which is not mutable, ie, you cannot add and delete nodes in this model. You can add and delete nodes in a DefaultTreeModel.
    You have to explicitly use/set a DefaultTreeModel to get this kind of behavior.

Similar Threads

  1. JTree Programmatic Node Expansion and Selection Probelm
    By hemanthjava in forum AWT / Swing
    Replies: 3
    Last Post: 01-16-2013, 08:23 AM
  2. Compile Trouble
    By adelgado0723 in forum New To Java
    Replies: 5
    Last Post: 04-21-2008, 02:02 AM
  3. trouble with program
    By jimJohnson in forum New To Java
    Replies: 1
    Last Post: 04-03-2008, 09:29 AM
  4. Jtree - making parts editable
    By kmarie in forum AWT / Swing
    Replies: 1
    Last Post: 07-27-2007, 02:34 AM
  5. how to display data in Jtree
    By paty in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 12:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •