Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-13-2008, 03:23 PM
Member
 
Join Date: Jul 2008
Posts: 26
aneesahamedaa is on a distinguished road
How to load a directory in JTree with Children(On expansion)
Hi,
How can I load a Jtree directory with children on expanding the node of that directory.
I have developed the following SSCCE. Please explain in its context.
My specific question is, when you execute the code given here, you can see the file bb.1 inside the directory bb.
bb.1 is a dummy which I used while creating the Jtree. When I expand bb, instead of the leaf bb.1 , I want to display some other file name, say qq.1 or jj.1.
What should I do?
Hope my question is clear.
Code:
//FileTreeFrame.java import java.util.ArrayList; import java.util.List; import javax.swing.JFrame; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; public class FileTreeFrame extends JFrame { private JTree fileTree; private FileSystemModel fileSystemModel; public FileTreeFrame(String abc) { super("JTree FileSystem Viewer"); // Build up your data List rootChildren = new ArrayList(); rootChildren.add( new MyNode("aa") ); List bbChildren = new ArrayList(); bbChildren.add( new MyNode("bb.1") ); rootChildren.add( new MyNode("bb", bbChildren) ); rootChildren.add( new MyNode("cc") ); MyNode rootNode = new MyNode("root", rootChildren); fileTree = new JTree(new FileSystemModel(new MyTreeNode(rootNode))); fileTree.setRootVisible(false); fileTree.setShowsRootHandles(true); getContentPane().add(fileTree); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(640, 480); setVisible(true); } public static void main(String args[]) { new FileTreeFrame(""); } } class FileSystemModel extends DefaultTreeModel { public FileSystemModel(DefaultMutableTreeNode node) { super(node); } public boolean isLeaf(Object node) { MyTreeNode treeNode = (MyTreeNode)node; return !((MyNode)treeNode.getUserObject()).hasChildren(); } } ======================================================= //MyNode.java import java.util.List; public class MyNode { private String name; private List children; public MyNode(String name) { this.name = name; } public MyNode(String name, List children) { this.name = name; this.children = children; } public boolean hasChildren() { return children!=null && children.size()>0; } public String toString() { return name; } public List getChildren() { return children; } } ========================================================= //MyTreeNode.java import java.util.Iterator; import javax.swing.tree.DefaultMutableTreeNode; public class MyTreeNode extends DefaultMutableTreeNode { public MyTreeNode(MyNode node) { super(node); addSubNodes(); } private void addSubNodes() { MyNode content = (MyNode)getUserObject(); if (content!=null && content.hasChildren()) { for (Iterator it = content.getChildren().iterator(); it.hasNext();) { add( new MyTreeNode((MyNode)it.next()) ); } } } }
Please help with a solution.
Regards,
Anees
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Move JTree item to another JTree. Melki AWT / Swing 7 09-08-2008 10:21 AM
bizarre auto expansion fishtoprecords Suggestions & Feedback 2 08-01-2008 12:52 AM
load all files in a directory moomoo New To Java 1 04-21-2008 12:18 PM
JTree Programmatic Node Expansion and Selection Probelm hemanthjava AWT / Swing 1 01-17-2008 08:36 AM
Communication with Expansion Slot Compact Flash device on PDA percivalwcy CLDC and MIDP 0 07-25-2007 11:04 AM


All times are GMT +3. The time now is 12:36 PM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org