Results 1 to 2 of 2
Thread: Object mismatch, not sure why
- 01-29-2011, 09:50 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 1
- Rep Power
- 0
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.
- 01-29-2011, 10:18 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
getChildAt() is declared to return a reference to a TreeNode not necessarily a reference to a DefaultMutableTreeNode. This is why the compiler is complaining. Basically it is saying "nodewrite is supposed to be a DefaultMutable TreeNode but you are assigning something that could be any sort of TreeNode!"Java Code:DefaultMutableTreeNode nodewrite; // ... nodewrite = rootNode.getChildAt(i);
You don't show the code where you add nodes to the tree, but if are sure that the result of getChildAt() will be a DefaultMutableTreeNode then you can cast the result:
Java Code:nodewrite = (DefaultMutableTreeNode)(rootNode.getChildAt(i));
This will result in a runtime error if the child node is not actually an instance of DefaultMutableTreeNode.
At the moment you are only printing the immediate children of the root node. (Actually you are only printing a newline, but you could do something to actually print the data contained in nodewrite). If those children can also contain children you will have to add code to visit those grandchildren etc.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?
DefaultMutableTreeNode offers a number of methods that return an enumeration of all of the children + grandchildren + etc that could be used. There is a lot of choice about the order in which you take a tree and list it in a linear fashion: depth first, preorder, postorder etc. These are mentioned in the API docs and you can find them described, eg, at Wikipedia.
Similar Threads
-
Type Mismatch error
By and0rsk in forum New To JavaReplies: 2Last Post: 10-10-2010, 11:16 AM -
printable area mismatch between Java & C++ possible?
By r00tb33r in forum AWT / SwingReplies: 0Last Post: 06-16-2010, 06:29 AM -
SQLException:java.sql.Exception:Data type mismatch in criteria expresion
By Dumisan in forum JDBCReplies: 6Last Post: 02-21-2010, 12:54 AM -
how to pass an arraylist from an object back to the parent object that it was created
By george_a in forum New To JavaReplies: 1Last Post: 03-04-2009, 06:14 PM -
type mismatch: cannot convert from double to float
By bugger in forum New To JavaReplies: 2Last Post: 11-16-2007, 01:24 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks