Algorithm for JTree?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Algorithm for JTree?

  1. #1
    Join Date
    Nov 2011
    Posts
    189

    Thumbs up Algorithm for JTree?

    Phew..Haven't worked with DevBook in quite some time (laziness sindrome) but i restarted the work and Im determined to do a lot in the next few days. I've made a pretty complicated algorithm that breaks down on this line

    if(!node.getParent().isRoot&&!node.isLeaf())

    The fist thing to notice is that the code won't compile, as getParent() doesen't seem to return a DefaultMutableTreeNode.
    I don't know much about casting, but i know it basically works like a general toString() method (correct me if I'm wrong) so I tried

    if(!node.getParent()(DefaultMutableTreeNode).isRoot&&!node.isLeaf())

    Since I'm here that didn't work either
    Now, I know you might ask for more code but trust me, there is nothing else to show..
    As a last clarification: I want to make this,node.getParent(), somehow, a compilable expression...
    Well..thanks for your time, cheers!

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Algorithm for JTree?

    I don't know much about casting, but i know it basically works like a general toString() method (correct me if I'm wrong) so I tried
    I've no idea what you mean by this.

    If you store an object in a variable of a super type of that object ie storing a String in a variable of type Object you can cast the object back to the original type (or any intermediate types, if there are any) by specifying the type in brackets to the left of the variable, ie:

    Code:
    Object myStrObject = "Test";
    String myStr = (String)myStrObject;
    As for your code, it's impossible to answer without knowing what type 'node' is.

    BTW shouldn't isRoot have brackets after it.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Nov 2011
    Posts
    189

    Re: Algorithm for JTree?

    Okay, so..
    isRoot() does indeed require brackets( the code is made directly here, it's. Ot pasted from the editor)
    Also node is a DefaultMutableTreeNode

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Algorithm for JTree?

    the code is made directly here, it's. Ot pasted from the editor)
    Don't do this, always paste the code you are actually using. It's hard enough trying to find out what's really wrong without having to sort out typos as well.

    getParent() doesen't seem to return a DefaultMutableTreeNode
    It returns TreeNode which is the interface all nodes must implement.

    If you know your nodes is a (or extends) DefaultMutableTreeNode then you can cast to that type ie:

    Code:
    if( !((DefaultMutableTreeNode)node.getParent()).isRoot() && !node.isLeaf() ) ...
    Which isn't very clear so you are better off writing the code as follows:

    Code:
    DefaultMutableTreeNode parent = (DefaultMutableTreeNode)node.getParent();
    
    if( !parent.isRoot() && !node.isLeaf() ) ...
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Nov 2011
    Posts
    189

    Re: Algorithm for JTree?

    Thanks for the lesson on casting, keang I used the first method as i prefer more compact code and it works as expected

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Algorithm for JTree?

    I used the first method as i prefer more compact code
    Oh dear.

    They both work as expected but only of them is easily understood.

    Writing compact code is not clever and it doesn't produce better, faster or more efficient code, it just makes the code far harder to read, understand and maintain.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center