Skip to main content

Jtree. Display of paths

3 replies [Last post]
Joined: 2003-06-12


I am trying to make simple image file browser. I am using Jtree and TreeModel to it. Everything works just fine but when the file tree is painted, full paths are shown for every folder / file. Is there an easy way to show only filename instead of full path?


to this:

Example code below:

public class FileTreeDemo {
public static void main(String[] args) {
// Figure out where in the filesystem to start displaying
File[] roots = File.listRoots();
File root = roots[0];

// Create a TreeModel object to represent our tree of files
FileTreeModel model = new FileTreeModel(root);

// Create a JTree and tell it to display our model
JTree tree = new JTree();

// The JTree can get big, so allow it to scroll.
JScrollPane scrollpane = new JScrollPane(tree);
// Display it all in a window and make the window appear
JFrame frame = new JFrame("FileTreeDemo");
frame.getContentPane().add(scrollpane, "Center");

* The methods in this class allow the JTree component to traverse
* the file system tree, and display the files and directories.
class FileTreeModel implements TreeModel {
// We specify the root directory when we create the model.
protected File root;
public FileTreeModel(File root) { this.root = root; }

// The model knows how to return the root object of the tree
public Object getRoot() {
return root;

// Tell JTree whether an object in the tree is a leaf or not
public boolean isLeaf(Object node) { return ((File)node).isFile(); }

// Tell JTree how many children a node has
public int getChildCount(Object parent) {
String[] children = ((File)parent).list(new ImageFileFilter());
if (children == null) return 0;

return children.length;

// Fetch any numbered child of a node for the JTree.
public Object getChild(Object parent, int index) {
String[] children = ((File)parent).list(new ImageFileFilter());
if ((children == null) || (index >= children.length)) {
return null;
//myFile myfile = new myFile((File)parent, children

//return myfile;
return new File((File)parent, children

// Figure out a child's position in its parent node.
public int getIndexOfChild(Object parent, Object child) {
String[] children = ((File)parent).list(new ImageFileFilter());
if (children == null){
return -1;
String childname = ((File)child).getName();

for(int i = 0; i < children.length; i++) {
if (childname.equals(children[i])) return i;
return -1;

public void valueForPathChanged(TreePath path, Object newvalue) {}
public void addTreeModelListener(TreeModelListener l) {}
public void removeTreeModelListener(TreeModelListener l) {}


Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2003-06-11
Joined: 2005-12-12

Either have your model do the magic filtering or maybe have the Render/Editor do it. I think the easiest attack point would be within the model.


Joined: 2003-06-12

> Either have your model do the magic filtering or
> maybe have the Render/Editor do it. I think the
> easiest attack point would be within the model.
> leouser

Thanks for a hint... and I attacked to my model :). I solved a problem like this:
Jtree uses toString method for displaying leaf. I modified my TreeModel and created my own Node class that extended File. Finally Node class overided toString method in away that it only prints filename.

Iam not sure was this easiest / best way to do it but it took only one extra class and few lines of code.