make a jtree from a directory
hi.i have some problems with making a jtree.i wanna make a jtree from a directory consist of some text file.i want to have the contain of files on the tree to.and i test all of parent and child nodes and all of them have right value but i dont know why i have an IllegalArgumentException.i dont know what to do now!!!can anyone help me?:-/
Re: make a jtree from a directory
Hello ostad,
I've made a program a few weeks back and I was waiting for an appropriate time to use it. If you would not mind testing it, it is a program designed to give the first few tips on posting here with some semi-dynamic results. It's pretty much a large amount of text that formats an output in the end, not complicated at all. Available here
Besides that, it is necessary for you to post the code that results in an error for us to help you, and the entire error message. An error message looks like this:
Code :
Main.java:12: cannot find symbol
symbol : method test()
location: class A
a.test();
^
1 error
Re: make a jtree from a directory
ok.this is my code and its error!
//*****************
public void listAllFiles(String directory, DefaultMutableTreeNode parent, Boolean recursive) throws IOException {
try {
File[] children = new File(directory).listFiles();
// list all the files in the directory
for (int i = 0; i < children.length; i++) { // loop through each
DefaultMutableTreeNode node = new DefaultMutableTreeNode(children[i].getName());
// only display the node if it isn't a folder, and if this is a recursive call
if (children[i].isDirectory() && recursive) {
parent.add(node);
listAllFiles(children[i].getPath(), node, recursive);
// call again for the subdirectory
} else if (!children[i].isDirectory()) { // otherwise, if it isn't a directory
parent.add(node);
}
root = new DefaultMutableTreeNode(children[i].getName());
FileReader infile = new FileReader(children[i].getPath());
BufferedReader reader = new BufferedReader(infile);
String s = reader.readLine();
treenode = new DefaultMutableTreeNode(s);
while (s != null) {
if (treenode.getParent() == null) {
treenode = new DefaultMutableTreeNode(s);
treenode.setParent(root);
par = (DefaultMutableTreeNode) treenode.getParent();
par.add(treenode);
} else {
treenode = new DefaultMutableTreeNode(s);
par = (DefaultMutableTreeNode) treenode.getParent();
par.add(treenode);
}
s = reader.readLine();
}
reader.close();
infile.close();
}
} catch (IOException e) {
JOptionPane.showMessageDialog(rootPane, "Error in reading file");
}
}
//**********
IllegalArgumentException:Argument is not a child!