JTree from vector

Hi there, i have looked back through the forums and at the JTree tutorial, and i have seen people mention about constructing a JTree from a vector.i can do this from a DefaultMutableTreeNode but how can one construct a JTree from a vector?.i have also seen from the api that you can supply a vector as an argument for the JTree constructor, but how does the JTree construct the tree from thia particular vector?many thanks.

but how does the JTree construct the tree from thia particular vector?Take a look at the JTree source code.

Similar Messages

  • Updating JTree from Vector

    Hello,
    i'm using a JTree and buildings its Tree from a Vector.
    Vector root = new Vector();
    Vector child1 = new Vector();
    Vector child2 = new Vector();
    child1.add(new String("Test1"));
    child1.add(new String("Test12"));
    child2.add(new String("Child2"));
    root.add(child1);
    root.add(child2);
    JTree myTree = new JTree(root);
    Now i have the problem that Item Test1 from child1 is changig to child2. Doing this at the vector level is not difficult, but how can i get the JTree taking this changes?

    JTree will lie dormant until it receives a nodesWereChanged, nodesWereDeleted or whatever event. This comes from the tree model. You will need to alert the tree's model that you have changed a node or two. Read the documentation.

  • Populating JTree from database

    Hi experts,
    I need some feedbacks about the way i populate my JTree from database, Is there any better alternatives?
    If yes, try explain it in detail.. I'm still new in Java.
    Sample database table:
    |       node_id      |       name      |       parent       |       hasChild      |
    |         1          |     garbage     |        null        |           1         |
    |         2          |       item      |          1         |           1         |
    |         3          |      mouse      |          2         |           0         |
    |         4          |       board     |          2         |           0         |
    |         5          |       item2     |          1         |           1         |
    |         6          |       item3     |          1         |           1         |
    |         7          |       pants     |          5         |           0         |
    -----------------------------------------------------------------------------------Here's the sample of my code:
    //... ResultSet rs = .....
    rs.last();
    int rowCount = rs.getRow(); //Get the total number of row in database table
    rs.beforeFirst();
    int i = 0;
    int j = 0;
    String [] nodeID = new String[rowCount];  //The node ID
    String [] parentID = new String[rowCount];  //A pointer to its parent ID
    DefaultMutableTreeNode [] node = new DefaultMutableTreeNode[rowCount]; //The node
    while(rs.next()) {
         String name = rs.getString("name");
         Boolean bool = rs.getBoolean("hasChild");
         node[i] = new DefaultMutableTreeNode(name,bool);
         parentID[i] = rs.getString("parent");
         nodeID[i] = rs.getInt("node_id") + "";
            //Set as root node when the pointer is null               
         if(parentID.equals("null")) {
              rootNode = node[i];
         i++;
    for(i=0; i<rowCount; i++) {
         for(j=0; j<rowCount; j++) {
              if(parentID[j].equals(nodeID[i]) && node[i] != null) {
                   node[i].add(node[j]);
    node[i] = null; //Obligates the parent after finish adding the child (to prevent unnecessary loops)
    //... tree = new JTree(rootNode);
    Thanks for the trouble,
    Regards,
    David

    The execution time are base on the code i post'ed here.
    Average over 30times using Vector = 640048 nanosecond to execute.
    Vector<String> nodeID = new Vector<String>();
    Vector<DefaultMutableTreeNode> node = new Vector<DefaultMutableTreeNode>();
    Vector<String> parentID = new Vector<String>();
    rs.last();
    int rowCount = rs.getRow();
    rs.beforeFirst();
    k=0;
    while(rs.next()) {
         String name = rs.getString("name");
         Boolean hasChild = rs.getBoolean("hasChild");
         node.addElement(new DefaultMutableTreeNode(name,hasChild));
         nodeID.addElement(rs.getInt("node_id")+"");
         parentID.addElement(rs.getString("parent"));
         if(parentID.elementAt(k).equals("null")) {
              rootNode = node.elementAt(k);
         k++;
    i=0;
    while(i<rowCount) {
         j=0;
         while(j<rowCount) {
              if(parentID.elementAt(j).equals(nodeID.elementAt(i)) && node.elementAt(j) != null) {
                   node.elementAt(i).add(node.elementAt(j));
                   if(!node.elementAt(i).getAllowsChildren())
                        node.insertElementAt(null,j);
              j++;
         i++;
    }Average over 30times using LinkedList = 95623 nanosecond to execute.
    //This section will have a class Nodes
    LinkedList<Nodes> gather = new LinkedList<Nodes>();
    while(rs.next()) {
         node = new DefaultMutableTreeNode(rs.getString("name"),rs.getBoolean("hasChild"));                    
         gather.add(new Nodes(rs.getInt("node_id") + "",node,rs.getString("parent")));
    for(Nodes findParent : gather) {                    
         if(findParent.getParentID().equals("null")) {
              findParent.setRoot(findParent.getNode());
              rootNode = findParent.getRoot();
         for(Nodes findChild : gather){
              if(findChild.getParentID().equals(findParent.getNodeID()))
                   findParent.getNode().add(findChild.getNode());               
    }As for the Array method i used from the previous post, the Average code execution time is x3 larger than the vector i used. So please do not use that method.
    Note: 1. The code i post can be improved further. 2. Execution time is based on individual computer.
    Well, I think i'll stop here. Anyone who wanted to know which method is more efficient currently, here's the answer.
    Hope that it helps anyone who have the problem here =).
    Edited by: DavidTW on May 23, 2011 3:14 PM
    Improved Version : Using for-each loop ( Effective Java, Second Edition, Joshua Bloch, Pg. 210).

  • JTree from Hashtable: weird ordering

    Need some help on this:
    I am making a JTree from a Hashtable:
    myJTree = new JTree(myHashtable);The elements are put in the hashtable in the right order, but they come out in the GUI all wrong, can't seem to see any logic behind the order.
    The children are not supposed to be sorted alphabetically, but in the order I get from an XML file. They are put in the Hashtable one by one.
    Any ideas?

    They are not sorted by key or value. But they are put in the hashtable in a specific order, the same order as they are in a XML file. And I need it to be in a data structure I can use to create a JTree.
    I tried to put the items in a vector. The order was then correct. The problem is that I then loose info (<key, value>) from the hashtable. And I cant get more than one level in the tree from the JTree(vector) constructor.
    So what do I do?
    To make it more clear how my tree looks like:
    ___Root object _nr1 (object in xml)
    |
    |________Child (first subcomponent in xmll)
    |
    |________Child (second subcomponent in xmll)
    |
    |________Child(third subcomponent in xmll)
    |
    etc.
    Its as simple as that

  • Removing a JTree from a JScrollPane

    I am working with an awt Applet but i need to use a JTree.
    When the user press a button a need to remove the JTree from its container but it doesn't work
    I try this:
    jscroll.remove(jtree);
    jscroll.updateUI();
    jtree = null;
    repaint();
    Can anybody help me, please.

    Thank you.
    Another question:
    I have a jtable with multiple spanded cells. I am using a custom jtable(the code is in http://www2.gol.com/users/tame/swing/examples/JTableExamples4.html) but i need insert a new row when you click on a button.
    I doing this:
    jtable.getModel().addRow(vector);
    but it throws an exception:
    Exception occurred during event dispatching:
    java.lang.ArrayIndexOutOfBoundsException: 11 >= 11
         at java.util.Vector.elementAt(Unknown Source)
         at javax.swing.table.DefaultTableModel.getValueAt(Unknown Source)
         at javax.swing.JTable.getValueAt(Unknown Source)
         at javax.swing.JTable.prepareRenderer(Unknown Source)
         at jp.gr.java_conf.tame.swing.table.MultiSpanCellTableUI.paintCell(MultiSpanCellTableUI.java:96)
         at jp.gr.java_conf.tame.swing.table.MultiSpanCellTableUI.paintRow(MultiSpanCellTableUI.java:68)
         at jp.gr.java_conf.tame.swing.table.MultiSpanCellTableUI.paint(MultiSpanCellTableUI.java:39)
         at javax.swing.plaf.ComponentUI.update(Unknown Source)
         at javax.swing.JComponent.paintComponent(Unknown Source)
         at javax.swing.JComponent.paint(Unknown Source)
         at javax.swing.JComponent.paintChildren(Unknown Source)
         at javax.swing.JComponent.paint(Unknown Source)
         at javax.swing.JViewport.paint(Unknown Source)
         at javax.swing.JComponent.paintChildren(Unknown Source)
         at javax.swing.JComponent.paint(Unknown Source)
         at javax.swing.JComponent.paintWithBuffer(Unknown Source)
         at javax.swing.JComponent._paintImmediately(Unknown Source)
         at javax.swing.JComponent.paintImmediately(Unknown Source)
         at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
         at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
         at java.awt.event.InvocationEvent.dispatch(Unknown Source)
         at java.awt.EventQueue.dispatchEvent(Unknown Source)
         at java.awt.EventDispatchThread.pumpOneEvent(Unknown Source)
         at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
         at java.awt.EventDispatchThread.run(Unknown Source)
    Can anybody help me

  • How to build n Tree from Vector

    Hi,
    i just want to build an n Tree from Vector.
    the Vector contains the elements {1,2,3,4}
    the tree should be like this (actually it should not be limited to 3 child, but just as an Example)
    i will be happy to use the jTree from Sun
    ++1
    +++2
    ++++3
    +++++4
    +++++4
    +++++4
    ++++3
    +++++4
    +++++4
    +++++4
    ++++3
    +++++4
    +++++4
    +++++4
    +++2
    ++++3
    +++++4
    +++++4
    +++++4
    ++++3
    +++++4
    +++++4
    +++++4
    ++++3
    +++++4
    +++++4
    +++++4
    +++2
    ++++3
    +++++4
    +++++4
    +++++4
    ++++3
    +++++4
    +++++4
    +++++4
    ++++3
    +++++4
    +++++4
    +++++4
    any Ideas?
    thanx.

    Massive crosspost. Answer here if you must.
    http://forum.java.sun.com/thread.jsp?forum=4&thread=444422

  • How do I use "Trim Paths" after "Create Shapes from Vector Layer" on a strokes only layer?

    Hello,
    Adobe After Effects CC
    2014.0.2
    Version 13.0.2.3
    I have imported an illustrator file that was created in Illustrator CC 18.0.0. The file contains the outline of a logo. It is the logo with the fill set to none and the stroke set to 1px.
    I drag the logo_strokes.ai file onto the composition, right click, and click Create Shapes from Vector Layer.
    A new layer is created and in the Contents "drop down", a Fill 1 layer is created.
    When I select the Contents "drop down" and click Add / Trim Paths, a Trim Paths 1 layer is created.
    When I animate Trim Paths 1, it appears to animate the Fill layer, not the stroke.
    How do I get it to animate just the stroke, and not show a fill at all?
    Thank you.

    Thanks,
    I am reading and watching tutorials. Still stuck on that one point.
    Here are the images of the After Effects layers. The illustrator layer is the top layer.

  • Building jtree from database query

    I am trying to build a JTREE from a database result-set. The resultset is listed below. I'm thinking I can somehow build a dynamic array of objects to build a TreePath, then use that to build the JTREE. Any input is appreciated.
    ResultSet Output:
    Here is how the results would be retrieved from the database (including the order of the output).
    usr 1
    local 2
    sbin 2 file4
    bin 3 file1
    bin 3 file2
    logs 3 file3
    tmpdir 4
    The first column is the directory name, the second column is the directory/file level/position, and the third column is the filename, if one exists.
    So from above:
    /usr/local/sbin has file "file4"
    /usr/local/bin has files "file1" and "file2"
    /usr/local/logs has file "file3" and directory "tmpdir"
    How could I gather this information into an appropriate structure and create a JTREE from it.
    Thanks.
    -Jim

    So each line would:basically...
    If I don't know the entire path of the parent (because I've read in a
    line that only has the "one-up" parent name) .... However, when I
    read the line from the database result-set, I don't know where "local" is.See... that's another problem with that data. Maybe you know cuz it's the last node if it's all in a proper order. In which case maybe you only need to hold the last parent node... Some recursive function would be useful.
    If I were going to store the data, I would store either the name as a full path:
    /usr
    /usr/local
    /usr/loca/bin
    or
    usr /
    local /usr
    bin /usr/local
    If you have that, it might be easier to figure out without having to worry about what belongs where.

  • Build a JTree from data

    Hello everyone,
    I have to build a JTree from a table data from database. The table structure is
    (id,parent_id,name). In order to build the tree, I have to the following 2 loop searching for the parent_id
    while (i<size){
    while(j<size){
    //find it's parent_id
    j++;
    i++;
    It is too slow! Can anyone help me?
    Thanks!
    --tc                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

    You just add children to whatever the parent node is for that object.DefaultMutableTreeNode root = new DefaultMutableTreeNode();
    child1 = new DefaultMutableTreeNode();
    root.add( child1 );
    grandChild = new DefaultMutableTreeNode();
    child1.add( grandChild );
    greatGrandChild = new DefaultMutableTreeNode();
    grandChild.add( greatGrandChild );Etc.

  • Problem loading JTree to Vector

    Can anyone tell how to load JTree to Vector?

    Assuming you used DefaultMutableTreeNode objects to construct your JTree, the following code gets all nodes of the tree and puts them into a Vector:DefaultMutableTreeNode root = (DefaultMutableTreeNode)theJTree.getModel().getRoot();
    Vector v = new Vector();
    Enumeration enum = root.depthFirstEnumeration();
    while (enum.hasMoreElements()) {
      v.add(e.nextElement());
    }Is that what you had in mind?

  • Unlink shapes from vector mask in PS CS 6

    Hi guys,
    Do you know how I can unlink the shape from vector mask like in Photoshop cs 4?
    cs6:
    cs4:
    This is especially interessting for if I want to move pattern overlay or something else. I hope you can help me.
    thx
    http://xd-artist.de

    It is relatively easy to create Collage PSD Template for Photoshop.  There are several ways to design them.  One way is to create image place holder layer with layer mask that you insert images into when you populate a copy of a template.  Photoshop support thousands of layers. 
    The way I do it is to user Alpha channels to map where images go. Photoshop only supports 53 Alpha channels so my collage templates are limited to 53 images. I populate my template using Photoshop scripts.  The Alpha channels are use to resize placed image files to fit the alpha channel sizes. Also to position the resized image and to virtually crop the resize images by adding the alpha channels to the image smart object layers. I have scripts that batch populate templates, Populate one and remain open in Photoshop for further tweaking. And a interactive script the allows you to select image as the template is being populated and tweak their placement and sizing.  The script also have options for adding text layer with filename over the images in one of 9 locations.  Layer styles can also be optionally added to image and text layers. Animated Templates are even possible.
    Photo Collage Toolkit: http://www.mouseprints.net/old/dpr/PhotoCollageToolkit.html
    One I populated today click on image for a Video Demo:

  • Creating shapes from Vector layers (.ai files)

    I am trying to make extrusions to an image.  I am new to Illustrator and have read that .ai files are the only ones that you can extrude in AE.  However when I bring in an .ai file and select create shape from vector layer, a grey square covers the shape.  I am assuming that it is a transparancy issue and it is just filling in the size of the .ai project.  I could be wrong though.  How can I fix this?

    You need a plain shape in AI. If you're using gradients, masks, clipping paths or other stuff it won't work. Please give us more info and possibly a screenshot of your ai file.
    Try this. Open up Illustrator. Take the pen tool and create a simple shape with a closed path. Import that single layer single path file into AE and see if it works. If it does, then show us a screenshot of your AI file and show all layers and expand out any groups. We'll try and help.

  • JTree from root

    If a TreeNode has been set as the root of a JTree, is there any method that will recover the JTree from the TreeNode (other than just coding the info into the program's data when you set the node as root)?

    I managed to come up with a pair of jtree/node classes that do what I want, e.g. node.addNotify(newChild) causes the jtree display to update; I think this is better than node.add(newChild);<something update the jtree display> on the basis that `an intelligent object moves complexity from the many to the few'(Cline & Lomow _C++ FAQS+ p 42).  I'm just surprised that this isn't made easier to do.

  • Jtree from database

    hii all,
    Does any one know hw to populate a Jtree from a database.pls hlep me with an example.
    thanx

    Hi there,
    Im having exactly the same problem. If you find out, please let me know, and I will do the same for you.
    Thanks
    Simon

  • Creating dynamically JTree from database values

    Hi,
    I have a local database with some node values. I receive these values from database as a String[].
    short example:
    String[] Values = {"mainNode","Processors","mainNode","RAM","mainNode","Monitors",
    "Processors","INTEL","Processors","AMD","RAM","Kingston","RAM","GoodRAM",
    "Kingston","400MHz","Kingston","433MHz"}First value is higher node, second is a child.
    I'd like to produce dynamically JTree from Values[] like below:
    MainNode
    |----Processors
          |----INTEL
          |----AMD
    |----RAM
          |----Kingston
                |----400MHz
                |----433MHz
          |----GoodRam
    |----MonitorsI can't build up any working and fast solution :(
    Can anyone help me ?
    Please for any advices (samples) which will help me to apply it.
    Dearly regards!

    This is a relatively straight forward task but it smacks of being homework so unless you post what you have already done you are unlikely to be given any code.
    As a hint -
    Go through the data creating a Map between the parent value and a child DefaultMutableTreeNode which contains as user object the child String.
    When you extract a parent String from the data lookup the parent DefaultMutableTreeNode in the map and add the child DefaultMutableTreeNode to the parent DefaultMutableTreeNode.
    Note - All the map is doing is giving you a quick way of looking up a DefaultMutableTreeNode given a parent name.
    Note - that your tree will have problems if the same value appears in two or more branches!

Maybe you are looking for