Results 1 to 2 of 2
  1. #1
    jasonwucinski is offline Member
    Join Date
    Feb 2011
    Posts
    64
    Rep Power
    0

    Default cast string to treeNode

    Hello,
    I'm trying to use the swing tree node control. I want to be able to add new nodes to a parent node dynamically but cant figure out how to cast from a string name to a treeNode. heres what i have so far:

    javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("root");
    javax.swing.tree.DefaultMutableTreeNode treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("District" );

    treeNode1.add(treeNode2);

    jTree1.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
    jTree1.setEditable(false);
    jTree1.setName("jTree1"); // NOI18N
    jTree1.setRootVisible(false);
    jTree1.setShowsRootHandles(true);
    jScrollPane1.setViewportView(jTree1);

    for (int i=0; i<arrayDistrict.length; i++){
    for (int j=0; j<1; j++){
    String NewTreeNodeName = "treeNode" +i;
    javax.swing.tree.DefaultMutableTreeNode NewTreeNodeName = new javax.swing.tree.DefaultMutableTreeNode("test");
    treeNode2.add(NewTreeNodeName);
    }
    }

    can anyone help?

    Thanks
    Jason

  2. #2
    jasonwucinski is offline Member
    Join Date
    Feb 2011
    Posts
    64
    Rep Power
    0

    Default

    never mind. I guess i didnt need to create a new object for each node.

Similar Threads

  1. equals() failing in userObject of TreeNode
    By PrinceSendai in forum AWT / Swing
    Replies: 1
    Last Post: 12-25-2010, 09:57 PM
  2. Replies: 6
    Last Post: 05-27-2009, 02:17 AM
  3. Cast From Image to String
    By Deepa in forum New To Java
    Replies: 4
    Last Post: 03-03-2009, 03:07 PM
  4. How to cast an Object into a specific type (Integer/String) at runtime
    By mailtogagan@gmail.com in forum Advanced Java
    Replies: 2
    Last Post: 12-03-2007, 02:04 PM
  5. Replies: 1
    Last Post: 08-02-2007, 05:07 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
  •