Home > Articles > Programming > Java

This chapter is from the book

This chapter is from the book

Using the File System Tree Control

The file system tree control is intended to be a black box. To use it, you decide on the starting point in the file system, then create an appropriate FileTree object.Usually, you will want to know when the user has selected a file in the tree. Since FileTree is derived from JTree, it supports the same events, so you can receive notification of user actions by registering a listener for the TreeSelectionEvent, which, as you know, is generated when any change is made to the selection. What you really want to know when you get one of these events is the file name or the File that corresponds to the node that has been selected, but the event itself only has TreePath objects associated with it. As you've already seen, you can get the complete set of objects that make up a TreePath by calling its getPath method. Once you've got these objects, you can invoke toString on each of them, then join them with the system file name separator character to get the full path name of the file. However, this would be a very slow process, so the method getPath-Name was added to FileTree. This method takes a TreePath from an event and returns the full path name that it represents, or null if it isn't a path in the tree. The implementation of this method, which is repeated from Listing 10-3, is very simple:

// Returns the full pathname for a path, or null
// if not a known path
public String getPathName(TreePath path) {
  Object o = path.getLastPathComponent();
  if (o instanceof FileTreeNode) {
    return ((FileTreeNode)o).file.getAbsolutePath();
  }
  return null; 
} 

First, the object the represents the last component of the TreePath is obtained using getLastPathComponent. If this TreePath was really obtained from an event generated by FileTree, the user object will be the FileTreeNode for the selected file. Before casting it, however, the code ensures that this is the case to protect against the application passing erroneous paths to getPathName. If the path really does correspond to a File-TreeNode, the full path name of the selected file is obtained from the File object which was stored in the file field when the node was constructed, and returned to the caller. If this is not a FileTreeNode, null is returned. For the case in which the application needs the File object for the selected node, there is a similar convenience method called getFile that will return it.

The test program that was used to demonstrate the FileTree control earlier in this chapter contains some typical code that shows how to handle file selections in the tree. You can try this code out by running the test program again using a command like this:

java JFCBook.Chapter10.FileTreeTest C:\ 

and clicking on file names in the tree. Here is what the relevant piece of code looks like:

final FileTree ft = new FileTree(args[0]); 

ft.addTreeSelectionListener(new TreeSelectionListener() {
  public void valueChanged(TreeSelectionEvent evt) {
    TreePath path = evt.getPath();
    String name = ft.getPathName(path);
    File file = ft.getFile(path);
    System.out.println("File " + name + " has been " 
      + (evt.isAddedPath() ? "selected" : "deselected"));
    System.out.println("File object is " + file); 
  } 
}); 

Having created the FileTree with its root at the location specified on the command line, a TreeSelectionListener is added to it. When the event occurs, the getPath method is used to extract the first path affected by the event. In this simple example, only one path will be handled—in the general case, you might need, if you allow it, to handle more than one simultaneous selection. The getPathName method of FileTree is invoked on the path to get the full path name of the selected file and the getFile method is called to demonstrate that the FileTree can also return a File object for the selected node. This is all you need to know about FileTree to make use of it for selecting files. Here is some typical output from this program:

File C:\WINDOWS has been selected 
File object is C:\WINDOWS 
File C:\RECYCLED has been selected 
File object is C:\RECYCLED 
File C:\Program Files has been selected 
File object is C:\Program Files 
  • + Share This
  • 🔖 Save To Your Account