Results 1 to 2 of 2
  1. #1
    olddog is offline Member
    Join Date
    Jan 2011
    Posts
    1
    Rep Power
    0

    Default 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.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Java Code:
    DefaultMutableTreeNode nodewrite;
    // ...
    nodewrite = rootNode.getChildAt(i);
    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!"

    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.

    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?
    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.

    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

  1. Type Mismatch error
    By and0rsk in forum New To Java
    Replies: 2
    Last Post: 10-10-2010, 11:16 AM
  2. printable area mismatch between Java & C++ possible?
    By r00tb33r in forum AWT / Swing
    Replies: 0
    Last Post: 06-16-2010, 06:29 AM
  3. Replies: 6
    Last Post: 02-21-2010, 12:54 AM
  4. Replies: 1
    Last Post: 03-04-2009, 06:14 PM
  5. Replies: 2
    Last Post: 11-16-2007, 01:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •