Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-31-2007, 03:59 PM
Member
 
Join Date: Jul 2007
Posts: 2
Alantie Vala is on a distinguished road
JTree trouble
Hello! i have following TreeModel (with some modifications):

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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-31-2007, 08:05 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,029
hardwired is on a distinguished road
Tell the TreeModel about the change. The tree listens to the model for changes.
You might try something like
Code:
((DefaultTreeModel)tree.getModel()).nodesWereRemoved(...args...);
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-31-2007, 10:08 PM
Member
 
Join Date: Jul 2007
Posts: 2
Alantie Vala is on a distinguished road
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/
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-01-2007, 12:12 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,029
hardwired is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Compile Trouble adelgado0723 New To Java 5 04-21-2008 03:02 AM
trouble with program jimJohnson New To Java 1 04-03-2008 10:29 AM
JTree Programmatic Node Expansion and Selection Probelm hemanthjava AWT / Swing 1 01-17-2008 07:36 AM
Jtree - making parts editable kmarie AWT / Swing 1 07-27-2007 03:34 AM
how to display data in Jtree paty New To Java 1 07-24-2007 01:28 AM


All times are GMT +3. The time now is 01:05 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org