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!

Similar Messages

  • 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.

  • Populating fields from database values

    I have a page being populated from database values. I used the Application Builder Wizard to create the application. One of the fields is displaying on the report, but when I click edit, the value is not there. The field is defined as a float and being displayed as a text item. I have created this Application twice with the same results.
    Any ideas?
    thanks!
    Christie

    Thank you for the quick reply!
    I turned on the debug, to ensure the automatic fetch
    process it on and it is. The edit page has 7
    fields, all are being populated except for this one.
    I have confirmed it is using the correct database
    column and it is replacing the existing value the
    same as the other fields.
    Any other ideas to what could be going on?You can see each page item assignment in the debug output. Look there to see where the page item is being set, and potentially unset I suppose. The thing to look for here might be page processes that might be interacting with that item.
    You could also simply drop the problematic page item and recreate it to see if you can make the value stick.
    Earl

  • 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).

  • Creating dynamic illustrations from spreadsheet

    Hi,
    I am currently working on a research project which involves creating dynamic graphics.
    My research involves the creation of a graphic diagram from spreadsheets/databases. But the output requires to be dynamic. E.g change the spreadsheet it changes the graphic.
    I am basically want to create my own graphs. But I don't want to use the illustrator graph wiz.
    I wondered if you had any suggestions of how to go about this? I have been looking into programing and plugins but nothing has come up so far. Can you help?
    Hope you can help!
    Ian Carr

    All i want to do is give shapes a vialue and that value
    corresponds to size colour and where it's placed on the page.
    What you describe can certainly be done with AI Javascript. But...
    My research involves the creation of a graphic diagram from spreadsheets/databases. But the output requires to be dynamic. E.g change the spreadsheet it changes the graphic.
    ...depends upon what exactly you mean by "dynamic; change the spreadsheet, it changes the graphic."
    With AI Javascript, you can:
    Parse values out of delimited text.
    Use those values to manipulate size, position, and color of objects.
    Assign Notes to objects, or send them to a Layer (or possibly use other mechanisms whereby to give them a "handle" by which to identify them again the next time you edit the values and rerun the script).
    So if by "dynamic" you mean to have the graph effectively "re-plot" itself upon demand after editing the values, yes.
    But if by "dynamic" you mean to watch individual objects move as you edit individual values in real time, without re-running the script, no. (That could be done in Flash.)
    JET

  • Help with dynamic checkbox from database results

    Hi,
    I am creating some dynamic checkboxes from the results of a database read to sqlite.  I am able to get the data and the correct number of checkboxes is being created but I'm having a problem populating the labels for the checkboxes.  Here is the section of code that gets the results from the database and creates the checkboxes.
                    var result:Array = stmt.getResult().data;
                    for each( var obj:Object in result )
                        var cb:CheckBox = new CheckBox();
                        cb.label = obj.toString();
                        cb.setStyle("color", 0x00593B);
                        cb.setStyle("symbolColor", 0x000000);
                        container.contentGroup.addElement(cb);                   
    Neil

    This was answered on another forum for me.
    The line cb.label = obj.toString();
    needed to be changed to the data.
    cb.label=obj.myDatabaseFieldName;
    myDatabaseFieldName would be whatever column you are pulling the data in from and should be the actual name in the table.
    Neil

  • How can I delete the site (created in webDB) from database?

    Hi I have the WebDB 2.2 and Database server Oracle8i and I created a lot of sites with WebDB but I do not know how can I delete them from database server. Could somebody help to me?

    Log into sql plus as webdb
    Drop user <site name>_admin cascade;
    Drop user <site name>_public cascade;
    Drop user <site name> cascade;
    and drop any other users you have made specically for this site.
    Remove the dad from the listener settings.
    If you wish to use this site name again in the same database you also have to delete a record from a table in the Webdb schema.
    I can't remeber what that is.....
    Good Luck
    Dave.
    null

  • 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 Dynamic JTree

    Hi everybody,
    I'm interested in creating GUI using JTree to display a records that read from the database.
    The previous-button and next-button is used to display previous and next records on the same JTree (Dynamic Tree). 20 different records will be displayed in the tree each time both buttons is clicked.
    When the node on the jtree is clicked the details of the record will be displayed onto other component may be textarea.
    How should I do that. Dynamic Tree?
    Any Help. Thanks you.

    I want to ...
    ... display them using a JTree....
    ... Do I need to use a JTree ? ...of course you do need a JTree if you want to display a JTree =0
    you would add/remove your users to the tree as nodes, check out http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html
    thomas

  • Dynamic menus from database with URL parameters

    Hello!
    I'm trying to build a set of dynamic menus that would be based on data from the database. I need to have a menu item for every database row, and all the items point to the same page, but each with its own parameter in the item's URL. The URLs in the menus would look like ".../form.jspx?recordID=xx", where xx is read from the database.
    Right now, I can display the menu, everything works fine, but the problem is in the PageMap section of Databindings.cpx - ADF needs to have every URL mapped to a page definition. So even if I have "form.jspx" mapped to "form_PageDef", I have to explicitly tell the server that "form.jspx?recordID=xx" is also mapped to "form_PageDef" by putting this into Databindings.cpx.
    Of course, if I have to manually enter page mappings for all the record IDs that appear in the menu, it's not really a "dynamic menu" anymore. Does anyone know of a way to avoid this problem?
    Can I somehow change Databindings.cpx programmatically? Can I change page mappings dynamically? Help, please? I'm very grateful for any and all info.

    Hi Vex,
    I'm not sure whether you'll find a solution as "Dynamic" as you are describing with dynamic entries via the DataBindings.cpx. Menu's work quite differently in JSF (See section 19.2 on dynamic menus in the developers guide). You may need to get the solution halfway.
    1) Drive the menu contents / structure from the database via some backing bean. The sample application that comes with JDeveloper has a fixed menu structure via exposed beans. You'd have to adapt this to be driven off of a managed bean. This takes a bit of work, but it can be accomplished. You'll need to loop through your database records via the Iterator to build the content of your menuModel
    2) Let JDeveloper create the entries in DataBindings.cpx to map the pageDef.xml file to each jspx file. (Manual). The pageDef.xml file gets interpreted during the initContext phase of the ADF Lifecycle, and the DataBindings.cpx tells this phase where to look to create the runtime version of the pageDef.xml (See developer's guide 13.2.3).
    3) Manually setup your faces-config with the right outcomes.
    4) have the database return the outcomes and viewIds, among other values.
    5) do the appropriate thing in your JSPX to make use of this menu module backing bean.
    In this case, you'll control the content and structure of the menu via the DB. However, you'd have to make sure the data in your DB matches the definitions of the jspx viewid's and outcomes.
    However, there is a quite a bit of work involved to do this. I.e. db design, backing bean for menu module, disciplined co-ordination between DB and JSF, etc...
    I hope this helps.
    Kenton

  • Creating dynamic caches from static config

    Hi, we normally create our caches using static config, using the std xml config.
    Example, in our cache-mapping, we'll have a cache like the below:
    <cache-name>account</cache-name>
                <scheme-name>distributed-persistent-write-thru</scheme-name>
                <init-params>
                    <init-param>
                        <param-name>cache-store-class-name</param-name>
                        <param-value>spring-bean:accountCacheStore</param-value>
                    </init-param>
                    <init-param>
                        <param-name>expiry-time</param-name>
                        <param-value>7d</param-value>
                    </init-param>
                    <init-param>
                        <param-name>high-units-param</param-name>
                        <param-value>6075000</param-value>
                    </init-param>
                    <init-param>
                        <param-name>low-units-param</param-name>
                        <param-value>4556251</param-value>
                    </init-param>
                </init-params>And in our schemes, we'll have something like:
       <distributed-scheme>
                <scheme-name>distributed-persistent-write-thru</scheme-name>
                <service-name>DistributedWriteThrough</service-name>
                <backing-map-scheme>
                    <read-write-backing-map-scheme>
                        <class-name>com.mycom.coherence.ExceptionLoggingBackingMap</class-name>
                        <internal-cache-scheme>
                            <local-scheme>
                                <scheme-ref>local-hybrid-eviction</scheme-ref>
                                <expiry-delay>{expiry-time}</expiry-delay>
                                <high-units>{high-units-param}</high-units>
                                <low-units>{low-units-param}</low-units>
                            </local-scheme>
                        </internal-cache-scheme>
                        <cachestore-scheme>
                            <class-scheme>
                                <class-name>{cache-store-class-name}</class-name>
                            </class-scheme>
                        </cachestore-scheme>
                        <write-delay>0</write-delay>
                    </read-write-backing-map-scheme>
                </backing-map-scheme>
                <autostart>true</autostart>
                <backup-count-after-writebehind>0</backup-count-after-writebehind>
            </distributed-scheme>However, now we need to be more dynamic. What i'd like to do is create N caches, that all look like the "account" cache above; so it's like i want to use the config as a template. I don't know how many of these caches i'll need, so i can't configure them statically. The number of caches will be given to the server at startup.
    Something like:
    List cacheList = new ArrayList<NamedCache>();
    NamedCache templateCache = CacheFactory.getCache("account");
    cacheList.add( templateCache );
    for( int i=0; i<count; i++) {
       cacheList.add( CacheFactory.createFromCache( templateCache, "account"+i ) );
    }So what's "best practice" for doing something like this?
    Thx.
    Edited by: user9222505 on Jul 9, 2012 4:52 PM

    Ahh.. I see. There are a few ways to do this.
    Presumably then you can create the cache store from Spring for a given cache name. So create a factory class with a static method that takes a String parameter, which will be the cache name, and returns a Cache Store. For example...
    package com.jk;
    public class CacheStoreFactory {
        public static CacheStore createCacheStore(String cacheName) {
            // in here goes your code to get the cache store instance from Spring
    }Now change your cache configuration to use the Factory for your cache store like this
    <cache-mapping>
        <cache-name>account*</cache-name>
        <scheme-name>distributed-persistent-write-thru</scheme-name>
        <init-params>
            <init-param>
                <param-name>expiry-time</param-name>
                <param-value>7d</param-value>
            </init-param>
            <init-param>
                <param-name>high-units-param</param-name>
                <param-value>6075000</param-value>
            </init-param>
            <init-param>
                <param-name>low-units-param</param-name>
                <param-value>4556251</param-value>
            </init-param>
        </init-params>
    </cache-mapping>
    <caching-schemes>
        <distributed-scheme>
            <scheme-name>distributed-persistent-write-thru</scheme-name>
            <service-name>DistributedWriteThrough</service-name>
            <backing-map-scheme>
                <read-write-backing-map-scheme>
                    <class-name>com.mycom.coherence.ExceptionLoggingBackingMap</class-name>
                    <internal-cache-scheme>
                        <local-scheme>
                            <scheme-ref>local-hybrid-eviction</scheme-ref>
                            <expiry-delay>{expiry-time}</expiry-delay>
                            <high-units>{high-units-param}</high-units>
                            <low-units>{low-units-param}</low-units>
                        </local-scheme>
                    </internal-cache-scheme>
                    <cachestore-scheme>
                        <class-scheme>
                            <class-factory-name>com.jk.CacheStoreFactory</class-factory-name>
                            <method-name>createCacheStore</method-name>
                            <init-params>
                                <init-param>
                                    <param-type>String</param-type>
                                    <param-value>{cache-name}</param-value>
                                </init-param>
                            </init-params>
                        </class-scheme>
                    </cachestore-scheme>
                    <write-delay>0</write-delay>
                </read-write-backing-map-scheme>
            </backing-map-scheme>
            <autostart>true</autostart>
            <backup-count-after-writebehind>0</backup-count-after-writebehind>
        </distributed-scheme>
    </caching-schemes>When Coherence comes to create a cache it will pass the name to the factory method to create the cache store.
    JK

  • Creating xml schema from database attributes

    Hi,
    I am trying to generate dynamic xml schemas from a java program.
    Please suggest me some solutions.
    The scenario is like this.
    The input to the java program is a table[oracle] name
    and based on that table's column headers, i need to define and
    create the xml schema dynamically.
    The purpose is that at a later point of time, i may
    need to copy the content of the database to xml files.
    Please reply with your suggestions.
    Thanks in advance,
    Dilip

    XML Schema itself is a well formed XML document. You can write a small utility class which can do this for you by using any XML API available in Java.
    Thanks,
    Tejas

  • Dynamic reading from database table

    Hi Experts,
    While reading from a database table the below statement for deletion works:
    DELETE (p_table) FROM <fs_wadbtab>.
    p_table: name of database table which is entered as a selection screen parameter
    <fs_wadbtab> : workarea of line type P_table
    However, the below statement does not work:
    READ (p_table) FROM <fs_wadbtab>.
    My requirement is to read a record from p_table with contents in a dynamic structure.
    Kindly suggest.
    Thanks.

    Just misunderstood you.
    Edited by: Karri Kemppi on Jun 23, 2010 10:00 AM

  • How to create scrolling graph from input values?

    Hi i'm new here, I'm trying to build a new concept for an interface inside an operating theatre - What I was trying to do (to start with) was make a line dynamically draw itself from a set of values (which I have in an excel spreadsheet) - gradually revealing itself over time like in the screens you see on medical drama's. How would I go about doing this?
    Thanks for your time,
    marksy
    PS Using CS4 if thats relevant

    Well first off you need to store your data somewhere, I suggest XML. Then you can use this data to draw a graph dynamically using the drawing api. To draw a single line you could do something like this.
    myClip.graphics.moveTo(0,0);
    myClip.graphics.lineTo(point1.x, point1.y);
    myClip.graphics.lineTo(point2.x, point2.y);
    To animate that you would simply need to interpolate a point that lies on the line connecting point1 and point2. Then you could ease your way to the end point (point2) and redraw the line at each frame. Its not very difficult to do once you are comfortable with the drawing api.

  • Dynamic hyperlink from database table

    hi,
    I have requirement to create left hand menu on a page. Menu label and hyperlink is stored in database. What is the best way to achieve this. Basically when user logs based on his user name query will fetch the labels and hyperlink source. How to achieve this in ADF? I could not find anything out of the box. I have view object which stores that information but i dont know how to convert that information into hyperlinks.

    Assuming the UrlInfo is the name of the View Object that has the attributes label & link, you can use the af:iterator to show the labels & links as follows:
    1) Drag & Drop the UrlInfo into the page as a table. Delete the table in the .JSPX page and keeping the pageDef as it is.
    2) Instead of the af:table, use af:iterator to show the same.
    .JSPX page sample:
    <af:iterator value="#{bindings.UrlInfo.collectionModel}" var="row"
    rows="#{bindings.UrlInfo.rangeSize}" id="t1">
    <af:goLink id="g1" text="#{row.label}"
    destination="#{row.link}"/>
    <af:spacer height="1px" id="sp1"/>
    </af:iterator>
    PageDef sample:
    <executables>
    <variableIterator id="variables"/>
    <iterator Binds="Employees" RangeSize="25"
    DataControl="AppModuleDataControl" id="UrlInfoIterator"/>
    </executables>
    <bindings>
    <tree IterBinding="UrlInfoIterator" id="UrlInfo">
    <nodeDefinition DefName="model.UrlInfoVO">
    <AttrNames>
    <Item Value="label"/>
    <Item Value="link"/>
    </AttrNames>
    </nodeDefinition>
    </tree>
    </bindings>
    Thanks,
    Navaneeth

Maybe you are looking for