How to develop a tree menu using Java Server Faces?

Posted on 2009-04-11
Last Modified: 2013-11-24
Could anyone guide me on how to develop a dynamic tree menu using Java Server Faces? The requirement is such that: There will be a root menu "Projects". When it is clicked, it should expand with a list of projects and each project should have a sub-menu with the modules related to that project. The nodes "Projects" and "Modules" are static, whereas the content should be dynamically loaded. Diagramatically, it should be as below:

Before Clicking "Projects":
+ Projects
After Clicking "Projects"
- Projects
     + ERP
     + Oracle
     + Java
After Clicking on "ERP"
- Projects
      - ERP
           + Modules
      + Oracle
      + Java

Can it be achieved without using "JavaScript"?
Question by:visu_nekk
  • 2
LVL 15

Expert Comment

ID: 24122071
I created a page for our project.We have used Apache Myfaces rather than reference implementation.

Attached is the image of my tree
private TreeModel hierarchyTree;

    private HierarchyNode hierarchyRootNode;

    public TreeModel getHierarchyTree()


        if(hierarchyRootNode == null)

  hierarchyRootNode = new HierarchyNode(0, "NONE", "MyRoot", false);


        hierarchyTree = new TreeModelBase(hierarchyRootNode); 


        return hierarchyTree;


public void setHierarchyTree(org.apache.myfaces.custom.tree2.TreeModel t) { this.hierarchyTree = t; }

private void addChildren(HierarchyNode currentNode)



        for(Iterator i = currentNode.getChildren().iterator(); i.hasNext(); )


            addChildren((HierarchyNode);//This is a recursive funtion


Getting Child Nodes from database


    public List getChildren()



        return children;


Loading the Children List

 public void loadChildren()


        if(childrenLoaded) return;

        Connection connection = null;

        PreparedStatement ps = null;

        ResultSet rs = null;



                String sql = "select * from Yourtable where PARENT_ID = ? ";

            connection = DB.getConnection();

            ps = connection.prepareStatement(sql);

            int i=1;

            ps.setInt(i++, id);

            ps.setString(i++, type);

            rs = ps.executeQuery();



                int cId = rs.getInt("CHILD_ID");

                String cType = rs.getString("CHILD_TYPE");

                String cName = rs.getString("CHILD_Name");

    HierarchyNode cNode = new HierarchyNode(cId, cType, cName, false);



            childrenLoaded = true;


        catch(Exception e) { log.error(e,e); }

        finally { DB.close(rs, ps, connection); }





                                    <t:tree2 value="#{bean.hierarchyTree}" id="hierarchyTree" var="node" showRootNode="false" preserveToggle="false" >//where bean is your bean.Make sure you spell the getter method exactly as in bean(case) 

                                        <f:facet name="hierarchy">


                                            <t:selectBooleanCheckbox value="#{node.checked}" title="#{node.description}"/>

                                            <h:outputText value="#{node.description}"/>





Open in new window


Author Comment

ID: 24174271
Is it possible to add a right click menu on the nodes using MyFaces? For example,
When I right click on West, a menu should be displayed with options "Add nodes to West" and "Delete West".Similarly  when I right click on East, a menu should be displayed with options "Add nodes to East" and "Delete East".
LVL 15

Expert Comment

ID: 24298415
I havent gone so far in JSF.Sorry

Accepted Solution

pips06 earned 500 total points
ID: 24533536
I suggest that you try Visual Web JSF framework cause it has some basic components and some advance components just like the tree component you can just drag and drop it to the stage and you can input your data there.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
topping3 challenge 14 70
Problem to start Neon 20 89
hibernate insert example 13 15
Why my table column Id is not passed to java object? 4 28
This is an explanation of a simple data model to help parse a JSON feed
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now