Du skal først have styr på din datastructur. Dernæst tager du den som udgangspunkt for at kreere din treemodel. Her er en relativ simpel treemodel bygget udfra en datastruktur (et filsystem):
package lib.gui;
import javax.swing.*;
import javax.swing.tree.*;
import java.io.*;
import java.util.Map;
import java.util.HashMap;
/**
* This adopted from the swing connection on java.sun.com. Ive added a simple DirectoryNode
* to this class. For allowing sorting of directories versus files, and to keep a buffer between
* the model and the listing of files. Seems to work remarkable faster when u have first listed
* the content of a directory. Next step would be to create nodes in advance. First though, I will
* test how well this fits into the app.
*
* @author Stig Tanggaard.
* @created 30. juni 2002
*/
public class FileSystemModel extends AbstractTreeModel implements Serializable {
String root;
private Map map;
/**
*Constructor for the FileSystemModel object
*/
public FileSystemModel() {
//this("
c://"); //if windows
this(System.getProperty("user.home"));
}
/**
*Constructor for the FileSystemModel object
*
* @param startPath Description of the Parameter
*/
public FileSystemModel(String startPath) {
root = startPath;
map = new HashMap();
}
/**
* Gets the root attribute of the FileSystemModel object
*
* @return The root value
*/
public Object getRoot() {
return new File(root);
}
/**
* Gets the child attribute of the FileSystemModel object
*
* @param parent Description of the Parameter
* @param index Description of the Parameter
* @return The child value
*/
public Object getChild(Object parent, int index) {
if (exists((File) parent)) {
SimpleDirectoryNode node = getNode((File) parent);
return node.getChild(index);
} else {
SimpleDirectoryNode node = addSimpleDirectoryNode((File) parent);
return node.getChild(index);
}
/*
* File directory = (File)parent;
* String[] children = directory.list();
* return new File( directory, children[index] );
*/
}
/**
* Gets the childCount attribute of the FileSystemModel object
*
* @param parent Description of the Parameter
* @return The childCount value
*/
public int getChildCount(Object parent) {
File fileSysEntity = (File) parent;
if (fileSysEntity.isDirectory()) {
String[] children = fileSysEntity.list();
return children.length;
} else {
return 0;
}
}
/**
* Gets the leaf attribute of the FileSystemModel object
*
* @param node Description of the Parameter
* @return The leaf value
*/
public boolean isLeaf(Object node) {
return ((File) node).isFile();
}
/**
* Description of the Method
*
* @param path Description of the Parameter
* @param newValue Description of the Parameter
*/
public void valueForPathChanged(TreePath path, Object newValue) {
}
/**
* Gets the indexOfChild attribute of the FileSystemModel object
*
* @param parent Description of the Parameter
* @param child Description of the Parameter
* @return The indexOfChild value
*/
public int getIndexOfChild(Object parent, Object child) {
File directory = (File) parent;
File fileSysEntity = (File) child;
String[] children = directory.list();
int result = -1;
for (int i = 0; i < children.length; ++i) {
if (fileSysEntity.getName().equals(children[i])) {
result = i;
break;
}
}
return result;
}
/**
* Method for determining wether a SimpleDirectoryNode of the argument file
* allready exists.
*
* @param file Description of the Parameter
* @return Description of the Return Value
*/
public boolean exists(File file) {
return map.containsKey(file.getPath());
}
/**
* Gets the node attribute of the FileSystemModel object
*
* @param file Description of the Parameter
* @return The node value
*/
public SimpleDirectoryNode getNode(File file) {
return (SimpleDirectoryNode) map.get(file.getPath());
}
/**
* Adds a feature to the SimpleDirectoryNode attribute of the FileSystemModel object
*
* @param file The feature to be added to the SimpleDirectoryNode attribute
* @return Description of the Return Value
*/
public SimpleDirectoryNode addSimpleDirectoryNode(File file) {
System.out.println(file.getPath());
SimpleDirectoryNode node = new SimpleDirectoryNode(file);
map.put(file.getPath(), node);
return node;
}
/**
* Needs some updating scheme.
*
* @author Stig Tanggaard
* @created 30. juni 2002
*/
class SimpleDirectoryNode extends Object {
File[] nodes;
/**
*Constructor for the SimpleDirectoryNode object
*
* @param file Description of the Parameter
*/
public SimpleDirectoryNode(File file) {
File[] files = file.listFiles();
nodes = new File[files.length];
int index = 0;
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
nodes[index] = files[i];
index++;
}
}
for (int i = 0; i < files.length; i++) {
if (!files[i].isDirectory()) {
nodes[index] = files[i];
index++;
}
}
}
/**
* Gets the child attribute of the SimpleDirectoryNode object
*
* @param index Description of the Parameter
* @return The child value
*/
public File getChild(int index) {
return nodes[index];
}
}
}