Results 1 to 2 of 2
  1. #1
    Tsiliadoros is offline Member
    Join Date
    May 2008
    Posts
    6
    Rep Power
    0

    Question Treepath to String

    Hi Everyone, i have a checkedbox JTree repsresenting the FileSystem. What i want is to get the paths in String representation from the nodes i check, here is a part of the code

    //the method i use
    public String getTreePath(TreePath treePath, String separator)
    {

    StringBuffer treePathStrBuff = new StringBuffer();
    String treePathStr = null;
    Object[] objs = treePath.getPath();
    if(objs != null)
    {
    // don't include the first path 'cos its root
    for(int i = 1; i < objs.length-1; i++)
    {
    treePathStrBuff.append(objs[i].toString());
    treePathStrBuff.append(separator);
    }
    treePathStrBuff.append(objs[objs.length-1].toString());
    treePathStr = treePathStrBuff.toString();
    }

    return treePathStr;
    }

    // and the call of the method
    String paths ="";
    paths = getTreePath((tr.getTree()).getSelectionPath(),the_ Seperator);
    JOptionPane.showMessageDialog(null,paths,"paths",J OptionPane.INFORMATION_MESSAGE);


    but what i get is somthing like this : FileTreePanelFileTreeNode@1bd747e and i want to get s/thing like this :C:\Documents and Settings\user\My Documents\My Pictures

    Is there a way to do it?
    Thanks a lot

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Use DefaultMutabltTreeNodes and for each TreePath:
    Java Code:
    DefaultMutableTreeNode node =
        (DefaultMutableTreeNode)path.getLastPathComponent();
    String s = node;  // Object [i]toString[/i] method is called
    // Retrieve a custom object if you prefer:
    YourType userObject = (YourType)node.getUserObject();
    // Override the [i]toString[/i] method in your custom user
    // object to return the string you want.
    System.out.println("userObject = " + userObject);

Similar Threads

  1. Replies: 2
    Last Post: 09-18-2008, 08:30 AM
  2. Replies: 0
    Last Post: 11-20-2007, 05:59 PM
  3. Replies: 1
    Last Post: 08-07-2007, 08:29 AM
  4. Replies: 1
    Last Post: 08-03-2007, 11:52 AM
  5. Help with String
    By Felissa in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 04:12 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
  •