Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

update jtree ??

109 views
Skip to first unread message

Mike T

unread,
Aug 16, 2002, 3:53:59 AM8/16/02
to
Hi,,

I have a filesystem using treemodel interface as it's model to JTree .. The
treemodel only use limited methods to only retrive from the filesystem..If i
put some new updating (add, edit) methods into the treemodel how can update
the JTree when the model is updated ..it should automaticly update my tree..
??

Thanks..

Give some code examples, please...

Mike


Linda Radecke

unread,
Aug 16, 2002, 4:25:19 AM8/16/02
to

Mike T wrote:

> Hi,,

> I have a filesystem using treemodel interface as it's model to JTree .. The
> treemodel only use limited methods to only retrive from the filesystem..If i
> put some new updating (add, edit) methods into the treemodel how can update
> the JTree when the model is updated ..it should automaticly update my tree..
> ??

Well, e.g. the DefaultTreeModel has insertNodeInto() which calls
nodesWereInserted(); there is removeNodeFromParent() which calls
nodesWereRemoved(), for example, as described in the documentation
and the tutorial. When you perform changes on your nodes, e.g.
setUserObject you'll then need to call afterwards:
treeModel.nodeChanged(changedNode); There is more information and
some samples on: http://www.chka.de/swing/tree/

Hope this helps


Linda
--
(=) li...@jalice.ch - http://www.jalice.net
/
(=) l.ra...@hswzfh.ch - http://www.hswzfh.ch

Thomas Weidenfeller

unread,
Aug 16, 2002, 4:38:57 AM8/16/02
to
"Mike T" <d...@tuncay.dk> writes:
> I have a filesystem using treemodel interface as it's model to JTree .. The
> treemodel only use limited methods to only retrive from the filesystem..If i
> put some new updating (add, edit) methods into the treemodel how can update
> the JTree when the model is updated ..it should automaticly update my tree..
> ??

Since you provide the implementation of TreeModel, and you are not
using an existing implementation, you need to to implement the
notification yourself. Which is not very difficult.

When you implement TreeModel you also implement addTreeModelListener().
This method is used by the JTree to register for change notifications.
You have to keep this registrations somewhere (e.g. in a list). The
interface used by the interested parties, like JTree, is called
TreeModelListener.

If you look at the documentation of TreeModelListener, you will see
that it provides four methods (tree node changed, was added, was
deleted, and tree structure changed). Whenever something in your model
has changed you are supposed to create a TreeModelEvent, go through all
your registered TreeModelListener, and call the corresponding
TreeModelListener method with the TreeModelEvent.

> Give some code examples, please...

Just look at the implementation of DefaultTreeModel.

/Thomas

Tuncay Altun

unread,
Aug 17, 2002, 3:43:51 PM8/17/02
to
Thank you very much,, but

Yes, since I'm using implementation of TreeModel. I need to implement the
notification by myself. But I think it's not so easy...could you help me
with some code , at least some code to be worked when I create some
folders/directories and the JTree updating it self .. , I'm stuck..

How, would you implement the notification on a tree ??

Below , I have the whole code for my Filesystem Model..

Please, help... I could send you more code if you want on my JTree...


// FileSystemModel.java
// TreeModel implementation using File objects as tree nodes.

public class FileSystemModel implements TreeModel
{

// hierarchy root
private File root;

// TreeModelListeners
private Vector listeners = new Vector();

// FileSystemModel constructor
public FileSystemModel( File rootDirectory )
{

root = rootDirectory;
System.out.println("Name of root :" + getRoot().toString());

}

// get hierarchy root (root directory)
public Object getRoot()
{
return root;
}

// get parent's child at given index
public Object getChild( Object parent, int index )
{
// get parent File object
File directory = ( File ) parent;

// get list of files in parent directory
String[] children = directory.list();

// return File at given index and override toString
// method to return only the File's name
return new TreeFile( directory, children[ index ] );
}

// get parent's number of children
public int getChildCount( Object parent )
{
// get parent File object
File file = ( File ) parent;

// get number of files in directory
if ( file.isDirectory() ) {

String[] fileList = file.list();

if ( fileList != null )
return file.list().length;
}

return 0; // childCount is 0 for files
}

// return true if node is a file, false if it is a directory
public boolean isLeaf( Object node )
{
File file = ( File ) node;
return file.isFile();
}

// get numeric index of given child node
public int getIndexOfChild( Object parent, Object child )
{
// get parent File object
File directory = ( File ) parent;

// get child File object
File file = ( File ) child;

// get File list in directory
String[] children = directory.list();

// search File list for given child
for ( int i = 0; i < children.length; i++ ) {

if ( file.getName().equals( children[ i ] ) ) {

// return matching File's index
return i;
}
}

return -1; // indicate child index not found

} // end method getIndexOfChild

// invoked by delegate if value of Object at given
// TreePath changes
public void valueForPathChanged( TreePath path,
Object value )
{
// get File object that was changed
File oldFile = ( File ) path.getLastPathComponent();

// get parent directory of changed File
String fileParentPath = oldFile.getParent();

// get value of newFileName entered by user
String newFileName = ( String ) value;

// create File object with newFileName to rename oldFile
File targetFile = new File( fileParentPath, newFileName );

// rename oldFile to targetFile
oldFile.renameTo( targetFile );

// get File object for parent directory
File parent = new File( fileParentPath );

// create int array for renamed File's index
int[] changedChildrenIndices =
{ getIndexOfChild( parent, targetFile) };

// create Object array containing only renamed File
Object[] changedChildren = { targetFile };

// notify TreeModelListeners of node change
fireTreeNodesChanged( path.getParentPath(),
changedChildrenIndices, changedChildren );

} // end method valueForPathChanged


// notify TreeModelListeners that children of parent at
// given TreePath with given indices were changed
private void fireTreeNodesChanged( TreePath parentPath,
int[] indices, Object[] children )
{
// create TreeModelEvent to indicate node change
TreeModelEvent event = new TreeModelEvent( this,
parentPath, indices, children );

Iterator iterator = listeners.iterator();
TreeModelListener listener = null;

// send TreeModelEvent to each listener
while ( iterator.hasNext() ) {
listener = ( TreeModelListener ) iterator.next();
listener.treeNodesChanged( event );
}
} // end method fireTreeNodesChanged


public boolean insertNewFolder( File newChild, File parent ) {

File newChild_ = newChild;
File parent_ = parent;

if (parent.isDirectory()) {

String selectedPathName = parent.getName();
System.out.println("VisibleDir : " + selectedPathName);

TreeFile newFolder = new TreeFile(parent_,
newChild_.toString().trim());
System.out.println("Creating folder "+newFolder.getPath());

if (!newFolder.mkdir())
return false;

}

return true;
}

// add given TreeModelListener
public void addTreeModelListener(
TreeModelListener listener )
{
listeners.add( listener );
}

// remove given TreeModelListener
public void removeTreeModelListener(
TreeModelListener listener )
{
listeners.remove( listener );
}

}

Thanks

Tuncay

Christian Kaufhold

unread,
Aug 17, 2002, 5:49:45 PM8/17/02
to
Hello!

Tuncay Altun <Tun...@tuncay.dk> wrote:

> How, would you implement the notification on a tree ??

See http://www.chka.de/swing/tree/ for everything.

> public class FileSystemModel implements TreeModel

> // get parent File object

These comments are rather superfluous.

> File directory = ( File ) parent;

> // get list of files in parent directory
> String[] children = directory.list();

You cannot know whether it is the same as last time. You need
to cache the children (see URL above).
(Even if list() would return "the same" everytime, it would
be horribly inefficient).

> // return File at given index and override toString
> // method to return only the File's name

Rather let a TreeCellRenderer handle that.

> return new TreeFile( directory, children[ index ] );

For example if children suddenly is smaller because a file
has been removed, you will cause an IndexOutBoundsException.

> // return true if node is a file, false if it is a directory
> public boolean isLeaf( Object node )
> {
> File file = ( File ) node;
> return file.isFile();

Files which are not "regular" files are not leaves that way.

return !file.isDirectory();

would be more sensible, but not correct due to silent changes.


> File file = ( File ) child;

No-one knows whether you are supposed to assume that "child" is a File
(or part of the tree model at all) here.

> // invoked by delegate if value of Object at given
> // TreePath changes

1. What does "delegate" mean here?
2. What does "value of Object" mean?
3. Incorrect. invoke as a suggestion to change that "value"
(see URL above what that means).

> // notify TreeModelListeners of node change
> fireTreeNodesChanged( path.getParentPath(),
> changedChildrenIndices, changedChildren );

You cannot use fireTreeNodesChanged for replacing an Object by another.
It can only be used for "internal" property changes. In fact, there is
no event for replacing at all (see URL above).


> private void fireTreeNodesChanged( TreePath parentPath,
> int[] indices, Object[] children )

Your event-firing code breaks standard assumptions, see (much) earlier
postings by me.
(Use AbstractTreeModel from above).

Christian

Tuncay Altun

unread,
Aug 18, 2002, 8:19:54 AM8/18/02
to
Hi Christian,

It seem that whole my code is useless and not efficient ? Could you just
tell me what models to use to explorer a filesystem with File as it's node
from : http://www.chka.de/swing/tree/ or other places ?? I just need a
JTree model which could retrive the filesystem and updating it in it's model
and view. Is't possible to tell me the name of models I could use or make a
demo of it and send to me ??

Thank you very much..


Tuncay

"Christian Kaufhold" <use...@chka.de> wrote in message
news:4t3d5ec25...@simia.chka.de...

Thomas Weidenfeller

unread,
Aug 19, 2002, 3:03:19 AM8/19/02
to
"Tuncay Altun" <Tun...@Tuncay.dk> writes:
> Thank you very much,, but

First, could you please decide on having one name, Tuncay Altun, or
Mike T.? After that, please read my original post.

> Yes, since I'm using implementation of TreeModel. I need to implement the
> notification by myself. But I think it's not so easy...could you help me
> with some code ,

How lazy are you? As I wrote in my original post:

>> Just look at the implementation of DefaultTreeModel.

It is rather simple, but as long as you refuse to pick up the help you
got you will not come very fare.

/Thomas

Christian Kaufhold

unread,
Aug 19, 2002, 6:52:10 AM8/19/02
to
Hello!

Tuncay Altun <Tun...@tuncay.dk> wrote:

> It seem that whole my code is useless and not efficient ? Could you just
> tell me what models to use to explorer a filesystem with File as it's node
> from : http://www.chka.de/swing/tree/ or other places ?? I just need a

Did you actually read the documents?

> JTree model which could retrive the filesystem and updating it in it's model
> and view. Is't possible to tell me the name of models I could use or make a
> demo of it and send to me ??

Of course, I have nothing better to do.


NOT!

Christian

0 new messages