Results 1 to 7 of 7
  1. #1
    summersab is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default DefaultTreeModel - populating and concatenation

    So, this isn't at ALL a Swing/GUI question, so I apologize up front. However, I know that the people that use these libraries most also do graphical data manipulation, so I figured this would be a good audience.

    I have a set of data similar to the following in 2-dimensional array of strings containing the following information (one primary array contains an array for each line; each tuple from each line is stored in a string, e.g. ArrayList<ArrayList<String>>):

    Java Code:
    ID		Term	ClassID
    100000205	1083	WCOB1023H
    100000205	1093	WCOB1033
    100033669	1109	MGMT4243
    100033669	1109	ACCT3533
    100050035	1103	WCOB1023
    100050035	1109	WCOB2033
    100079394	1079	ISYS2263
    100083048	1053	FINN3043
    What I want to do is take this information and put it in a tree structure. ID is the root hierarchy, Term is second hierarchically, and ClassID is third. Obviously, there is a lot of redundancy - a single student may have been enrolled in multiple terms, and within those terms, a student can take multiple classes. How do I best go from what I have up there to:

    Java Code:
    100000205
    	1083	
    		WCOB1023H
    	1093
    		WCOB1033
    100033669
    	1109
    		MGMT4243
    		ACCT3533
    100050035
    	1103	
    		WCOB1023
    	1109
    		WCOB2033
    100079394
    	1079
    		ISYS2263
    100083048	
    	1053
    		FINN3043
    Does that make sense? There's not a whole lot of clear reference or examples regarding non-graphical use of the DefaultTreeModel class (or even JDots). If someone could show me how to a) populate a hierarchy in a tree and perhaps b) how to concatenate tree structures from populated hierarchies, that would be extremely helpful.

    Thanks, and I appreciate this forum!

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    While I'm not perfectly clear as to your requirements, it sounds like a more appropriate design is to us an object-oriented approach - create classes which define your data model rather than having lists/trees with disparate pieces of information. Using this design one can more readily understand the data and the data model as well as traverse said model to mine the data.

  3. #3
    summersab is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    This is true - I have tried this approach, actually, but the application I have needs to be extensible beyond my skill set. Since I don't want to reinvent the wheel, I figured I could use an existing library.

    Let me simplify my question, I guess. First, I'm not really sure how the DefaultTreeModel structure works. How do you make a simple hierarchy with nodes and such? I haven't found a good tutorial with regards to this.

    Second, is it possible to concatenate tree structures as defined by the TreeModel interface? For instance, if I had a hierarchy as follows:

    Java Code:
    		Student 1
    		|	|
    	Class 1		Class 2
    	|		|		|
    Detail 1		Detail 1	Detail 2
    Is there a way to combine this with the following hierarchy:


    Java Code:
    Student 1
    	|		
    Class 1
    	|
    Detail 2
    To get the following:

    Java Code:
    		Student 1
    		|	|
    	Class 1		Class 2
    	|	|	|		|
    Detail 1 Detail 2	Detail 1	Detail 2
    Sorry for the really ugly ASCII art - I can draw it and attach it if that doesn't make sense.

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    First, the DefaultTableModel wasn't written to be implemented like a tree, so the structure should be based upon a linear data structure like a List or an array. Second, I don't see a need for the model if you design your application appropriately. A TreeModel might be better to use for treelike data, but regardless of whether you write your own tree or use a TreeModel you will need to know how to traverse the data structure to retrieve your data and/or append data in the appropriate location (as you describe you wish to accomplish).
    but the application I have needs to be extensible beyond my skill set
    I'm not sure what you mean by this exactly...I'll reiterate that the easiest manner to write this is to use object-oriented programming....classes contains a List of students, etc...this alone defines the data structure you want without all the confusing and abstract models you seem to be trying to go after.

  5. #5
    summersab is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by doWhile View Post
    First, the DefaultTableModel wasn't written to be implemented like a tree, so the structure should be based upon a linear data structure like a List or an array.

    ...

    A TreeModel might be better to use for treelike data.
    Your reply kinda makes me smile. I think you need to reread my title - DefaultTREEModel. That's what have been talking about. I guess you just misread my post.

    With that said, can you point me in the right direction to populate a DefaultTreeModel type? Something like this:

    Java Code:
    DefaultTreeModel output = new DefaultTreeModel();
    ArrayList<String> input; //This contains all my input data in hierarchical order.
    for (int i = 0; i < ArrayList.size(); i++) {
    	//add the element to the tree
    }
    Once I do this for every element, it would be NICE to be able to merge them all (if the TreeModel classes have this capability). By "merging," I mean adding all elements that don't currently exist in one hierarchy to the appropriate place in the other without overwriting existing elements. As with my example, merging two existing student records would simply add in the child elements that don't exist. I don't know if this is available (perhaps it is in JDots?), but it would be a lot easier than

    Java Code:
    if (tree1node.getPathToRoot != tree2node.getPathToRoot)
    	//add element to tree1
    else
    	//look at level down in hierarchy
    Is this any more clear? I feel like we're totally miscommunicating, here. Oh, and as far as writing my own data structures . . . I could, but it got hairy when trying to implement the possibility of infinite children and traversing the elements. Since DefaultTreeModel SEEMS to do this, I'd like to use it (I just need to know HOW . . . )

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    Indeed I did misread...my apologies. Beyond the basic java tutorial on JTree's which contains the basics for constructing the model via a DefaultMutableTreeNode (How to Use Trees (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components) ), there are tons of tutorials out there for how to use trees in the most basic sense - in fact the basics of tree's is a very important skill for a programmer to have so if you are unfamiliar with the techniques of insertion/deletion/merge/traversal/etc... I recommend learning them. To merge, you will have to do this manually - traverse down from the root node looking for node equality, then populate the tree as you see fit.

  7. #7
    summersab is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    Haha - not a problem. I just gave the page that crooked confused dog look for a minute. Glad we're talking about the same thing this time :)

    I've found that link and several others. The problem I run into is that they pretty much ALL refer to GUI/Swing implementations and JTrees, and there's a lot of javax specific code mixed in. (Okay, okay, I'm being a little baby in that I don't want to wade through code and pick out the necessary parts). I understand that the basic functionality of JTrees is the same, but I struggled to find a decent tutorial that showed examples of how each function is used with outputs and such (I'm a very kinesthetic learner). I guess I can just play until if works, but I hoped to see good, clean examples and understand the functions before coding.

    As for concatenation . . . I kinda figured as much. I have some logic that should handle that, but it's not tested, and built in would have simplified things.

    Thanks though - you've been a great help!

Similar Threads

  1. Concatenation with strings
    By link6790 in forum New To Java
    Replies: 16
    Last Post: 05-05-2011, 07:33 PM
  2. Replies: 4
    Last Post: 08-19-2010, 01:06 AM
  3. Concatenation file contents
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 02-07-2008, 01:29 PM
  4. Database Column vs String Concatenation
    By varun_33 in forum JDBC
    Replies: 1
    Last Post: 12-11-2007, 04:14 PM

Tags for this Thread

Posting Permissions

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