This is my TreeModel Class
BusinessLayer;
import java.io.File;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
/**
*
* @author attam
*/
public class FileTreeModel implements TreeModel
{
// Attributes
private File root;
/**
* Constructor initializes the root of TreeModel
* @param root directory
*/
public FileTreeModel(File root)
{
this.root = root;
}
/**
* Returns root of the TreeModel i.e. JTree
* @return root
*/
public Object getRoot()
{
return root;
}
/**
* Check if given node is root or not
* @param node the node to be checked
* @return true if it is root else false
*/
public boolean isLeaf(Object node)
{
return ((File)node).isFile();
}
/**
* Returns number of child nodes in JTree
* @param parent Parent node
* @return no of child nodes
*/
public int getChildCount(Object parent)
{
String[] children = ((File)parent).list();
if (children == null) return 0;
return children.length;
}
/**
* Returns the child node by its index
* @param parent Parent Node
* @param index Index of child node
* @return child node
*/
public Object getChild(Object parent, int index)
{
String[] children = ((File)parent).list();
if ((children == null) || (index >= children.length))
return null;
return new File((File) parent, children[index]);
}
/**
* Return index of child node
* @param parent Parent of child
* @param child child node
* @return index of child node
*/
public int getIndexOfChild(Object parent, Object child)
{
String[] children = ((File)parent).list();
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;
}
@Override
public void valueForPathChanged(TreePath path, Object newvalue) {}
@Override
public void addTreeModelListener(TreeModelListener l) {}
@Override
public void removeTreeModelListener(TreeModelListener l) {}
}
this is TreeModel class where i declear tree functinality
this is the class from where the filechooser function is called
package BusinessLayer;
import java.io.File;
import javax.swing.JFileChooser;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author Mohsin Atta
* @Reg # 3258-BSSE/F16
*/
public class FileInformation {
public static JFileChooser fileChooser = new JFileChooser();
public static List<File> allFiles;
public static String folderName;
public static File getAllFiles() {
List<File> fileList=new ArrayList<>();
// JButton open = new JButton();
if(fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
{
File files[] = fileChooser.getCurrentDirectory().listFiles();
fileList= Arrays.asList(files);
allFiles=fileList;
return fileChooser.getCurrentDirectory();
}
return null;
}
public static List<File> getJavaFiles() {
List<File> fileList=new ArrayList<>();
for(File file:allFiles)
{
if(file.getName().endsWith(".java"))
{
fileList.add(file);
ClassAndMethodExtractor obj = new `ClassAndMethodExtractor(file);`
obj.getPublicClass();
obj.getPublicMethods();
}
}
return fileList;
}
}
in above file in getAllFiles function
and this is the function where I set the tree but not dispalyed the tree
public static void setFileList(File file) throws IOException
{
String setFolder;
// setFolder = folder+" Accessed";
if (file != null)
{
TreeModel model = new FileTreeModel(file);
tree.setModel(model);
//allFilesArea.append(file.getCanonicalPath());
//allFilesArea.append("\n");
}
}
i face the problem that is i declare the tree and also use it according to method but still it doesn't show the tree and also it doesn't give any error.. so kindly give the solution or any other alternative to make tree of File path