Object mismatch, not sure why
HI,
I am new to Java and trying to learn how to use trees. I've downloaded the DynamicTreeDemo project from the Oracle/Sun website and am modifying it.
I want to print the tree to a file, and have added a method to the DynamicTree class. Netbeans keep giving me an error at the 'nodewrite = ..' statement, when I try to use rootnode, saying
incompatible types
required: javax.swing.tree.DefaultMutableTreeNode
found: javax.swing.tree.TreeNode
--
// write the tree here
public void WriteData(File selectedfile){
File outfile = selectedfile;
DefaultMutableTreeNode nodewrite;
try {
PrintWriter out = new PrintWriter(
new BufferedWriter(
new FileWriter(outfile)));
int childcount = rootNode.getChildCount();
for (int i = 0; i < childcount; i++ ){
nodewrite = rootNode.getChildAt(i);
out.println();
}
}
rootNode is defned as a variable at the top of DynamicTree, so:
public class DynamicTree extends JPanel {
public DefaultMutableTreeNode rootNode;
protected DefaultTreeModel treeModel;
protected JTree tree;
private Toolkit toolkit = Toolkit.getDefaultToolkit();
I don't really understand why netBeans thinks there is a type mismatch.
Also, I'm thinking in terms of looping over the nodes like looping over an array, and printing each node as a string. is there a better way, like using enumeration to loop over the nodes?
I'm only using netBeans as the editor, as I find I can't understand the code it generates.