aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes getting first child after root in JTree Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "getting first child after root in JTree" Watch "getting first child after root in JTree" New topic
Author

getting first child after root in JTree

Isaac Hewitt
Ranch Hand

Joined: Jul 24, 2006
Posts: 190

I am at my wit's end in trying to find away to know if a user has clicked on the first child of a JTree. My tree has directories from A to Z. Inside
those dorectories are other directories. I want to be able to disable the popup menu items if the user clicks on a dir from A to Z. I could just use the String class to find this out except
sometimes there is a directory inside one of the leter directories that can start with just a single capital letter. I think it should be easier just to the find the child directories of the ROOT.
I cannot cast a node to Default MutableTreenode because I am suing the JTree provide by TrueZip classes for file encryption. I have tried playing with the following lines of code for an hour or so:

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Isaac Hewitt wrote:I think it should be easier just to the find the child directories of the ROOT.

Yes. That should be the first step. Then you can see if the node just selected was one of them.
I cannot cast a node to Default MutableTreenode because I am suing the JTree provide by TrueZip classes for file encryption.

But that's not quite the point. You need to get the children of the root, no matter what class is implementing them. Surely this JTree version has a way to do that?

Edit: Actually you don't need to find the children of the root. What you need (I think) is simply to ask whether the parent of the selected directory is the root of the tree.
Isaac Hewitt
Ranch Hand

Joined: Jul 24, 2006
Posts: 190


Edit: Actually you don't need to find the children of the root. What you need (I think) is simply to ask whether the parent of the selected directory is the root of the tree.

I previously had that idea too, but was not able to find the way to do this either. I want to be able to detect this so I can disable the rename command in the menu item so the user cannot rename the directories from A to Z. So it's back to the drawing board.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Why not? Doesn't your node class have a getParent() method?

(Note that nobody out here knows anything about the classes you're using. So when you don't provide any details about them, it's hard to say much.)
Isaac Hewitt
Ranch Hand

Joined: Jul 24, 2006
Posts: 190



The above code never gets to the print out "eureka" obvioulsy because the Strings don,t match. I am however getting close to cracking it albeit clumsily.
Isaac Hewitt
Ranch Hand

Joined: Jul 24, 2006
Posts: 190

A breakthrough, but still seems like too much code. Thank you for your help.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getting first child after root in JTree