Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8251983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:24:58+00:00 2026-06-08T00:24:58+00:00

I have a database table named tree1, which has a table names student_details.This table

  • 0

I have a database table named tree1, which has a table names student_details.This table has 3 columns student_name,student_details,student_phone

Now, am able to retrieve student details from the database but only in static way.

Question is when I insert a new student detail row {student_name,student_details,student_phone} I want it to show on my Jtree. Although Hashmap is the solution I am not able to understand how to use hashmap in a Jtree to create dynamic nodes.

below is the code from which I could do a static Jtree, and i want to make it dynamic. Can anyone tell me how to do this with a code sample?

package tree_try;


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.beans.Statement;
import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.*;


public class SimpleTree extends JFrame
{
   public Connection connect = null;


   public Statement statement = null;
   public Statement statement2 = null;
    public Statement statement3 = null;
      public Statement statement4 = null;
   public Statement statement5 = null;
    public Statement statement6 = null;

   public ResultSet resultSet = null;
   public ResultSet resultSet2 = null;
    public ResultSet resultSet3 = null;
     public ResultSet resultSet4 = null;
   public ResultSet resultSet5 = null;
    public ResultSet resultSet6 = null;



   public ArrayList arrayList = new ArrayList();
   public ArrayList arrayList2 = new ArrayList();
   public ArrayList arrayList3 = new ArrayList();
   public ArrayList arrayList4 = new ArrayList();

   static String store[] = new String[10];
   static String store2[] = new String[10];
   static String store3[] = new String[10];

   //for the panel

   static String store4[] = new String[10];
   static String store5[] = new String[10];
   static String store6[] = new String[10];
    static String store7[] = new String[10];

   //for panel over


   int i=0;
   int i1=0;
   int i2=0;
   int i3=0;



    JPanel jp1 = new JPanel();
    JPanel jp2 = new JPanel();
    JFrame jf1 = new JFrame();

    JButton jb1 = new JButton("Save");
       JButton jb2 = new JButton("Cancel");

    JTextField jt1= new JTextField();
    JTextField jt2 = new JTextField();
    JTextField jt3= new JTextField();
    JTextField jt4 = new JTextField();



    JLabel jl0 = new JLabel();

    JLabel jl1 = new JLabel("Name : ");
    JLabel jl2 = new JLabel("Adress : ");
    JLabel jl3 = new JLabel("Phone Number : ");
    JLabel jl4 = new JLabel("Other Deatils : ");
    JLabel jl5 = new JLabel("");


  public static void main(String[] args)
  {
    new SimpleTree();
  }

  public SimpleTree() 
  {

    super("Schools database");
    WindowUtilities.setNativeLookAndFeel();
    addWindowListener(new ExitListener());





     //db part
      try 
                {
            Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
            connect = DriverManager.getConnection("jdbc:derby://localhost:1527/treedata1","suraj","suraj");
            PreparedStatement statement = connect.prepareStatement("SELECT * from school_details");
                        PreparedStatement statement2 = connect.prepareStatement("select student_name from student_details where s_id =1");   
                         PreparedStatement statement3 = connect.prepareStatement("select student_name from student_details where s_id =2");   

                         //for the panel display


                         //PreparedStatement statement5 = connect.prepareStatement("select student_name from student_details where s_id =1");   
                         //PreparedStatement statement6 = connect.prepareStatement("select student_name from student_details where s_id =2");



            resultSet = statement.executeQuery();
            while (resultSet.next()) 
                        {
                String sname = resultSet.getString("school_name");
                String sid = resultSet.getString("s_id");
                arrayList.add(sname);
                                System.out.println("this is stsement one"+sname);
                                //arrayList.add(number);

            }

                        resultSet2 = statement2.executeQuery(); 
                        while (resultSet2.next())
                        {
                                String user2 = resultSet2.getString("student_name");
                //int  number2 = resultSet2.getInt("s_id");
                                arrayList2.add(user2);
                                System.out.println("this is stsement two"+user2);
                        }
                      //  System.out.println(arrayList); 
                         resultSet3 = statement3.executeQuery(); 
                        while (resultSet3.next())
                        {
                                String user3 = resultSet3.getString("student_name");
                //int  number2 = resultSet2.getInt("s_id");
                                arrayList3.add(user3);
                                System.out.println("this is stsement three"+user3);
                        }
                        System.out.println("this is after statement 3 before 4");



        } 
                catch (Exception e2) 
                {
            e2.printStackTrace();
        } 





      //
    Iterator it = arrayList.iterator();
    while (it.hasNext()) 
    {
         store[i]= (String) it.next();
         i++; 
                 //System.out.println(it.next());
    }


    Iterator it2 = arrayList2.iterator();
    while (it2.hasNext()) 
    {
         store2[i1]= (String) it2.next();
         i1++;        
        //System.out.println(it.next());
    }

     Iterator it3 = arrayList3.iterator();
    while (it3.hasNext()) 
    {
         store3[i2]= (String) it3.next();
         i2++;        
       // System.out.println(it.next());
    }








    // ------------------------- Visible Settings start here --------------------//

     Object[] hierarchy ={"Click for schools",new Object[] {store[0],new Object[] { "Student Details",store2[0],store2[1] } },new Object[] { store[1],new Object[] { "Student Details",store3[0],store3[1]}}};
    DefaultMutableTreeNode root = processHierarchy(hierarchy);
    final JTree tree = new JTree(root);

   // setSize(275, 300);
    jp1.setSize(50,50);
    jp1.setBackground(Color.WHITE);
    jf1.setExtendedState(Frame.MAXIMIZED_BOTH);
    jf1.setLayout(new GridLayout(1,2));
    jf1.setVisible(true);
    jf1.add(new JScrollPane(tree), BorderLayout.WEST);
    jf1.add(jp1);
    jp1.setLayout(null);

  jl0.setBounds(10,1,500,100);  
  jp1.add(jl0);


 jl1.setBounds(55,90,150,100);   
 jt1.setBounds(225,130, 155, 25);

 jp1.add(jl1);
 jp1.add(jt1);

 jl2.setBounds(55,160, 150, 100);
 jt2.setBounds(225,200, 155, 25);

 jp1.add(jl2);
 jp1.add(jt2);

 jl3.setBounds(55,230,150,100);
 jt3.setBounds(225,270, 155, 25);

  jp1.add(jl3);
  jp1.add(jt3);

 jl4.setBounds(55,295, 150, 100);
 jt4.setBounds(225,330, 155, 25);

  jp1.add(jl4); 
  jp1.add(jt4);

  jb1.setEnabled(false);
  jb2.setEnabled(false);

 jb1.setBounds(150,430, 100, 50);
 jb2.setBounds(350,430, 100, 50);

  jp1.add(jb1);
  jp1.add(jb2);

  //-----------------Visible setting stop here--------------------------//

   //------------------- Element actions here------------------------//


    jt1.addFocusListener(new FocusListener() {

            @Override
            public void focusGained(FocusEvent e) 
            {
             jb1.setEnabled(true);
             jb2.setEnabled(true);    
            }

            @Override
            public void focusLost(FocusEvent e) 
            {
                jb1.setEnabled(false);
                jb2.setEnabled(false); 
            }
        });

    //now for the tree

    tree.addTreeSelectionListener(new TreeSelectionListener() {

            @Override
            public void valueChanged(TreeSelectionEvent e) {
                jf1.dispose();
                //jl0.setText("Displaying information About :  "+tree.getLastSelectedPathComponent().toString());
                store7[0]= tree.getLastSelectedPathComponent().toString(); 
                System.out.println("in store 7 of 0"+store7[0]);
                dbaction db = new dbaction(store7[0]);
            }
        }); 

   }
//------------------------------------end of action listening-------------------------

  private DefaultMutableTreeNode processHierarchy(Object[] hierarchy) 
  {
    DefaultMutableTreeNode node = new DefaultMutableTreeNode(hierarchy[0]);DefaultMutableTreeNode child;
    for(int i=1; i<hierarchy.length; i++) 
    {
      Object nodeSpecifier = hierarchy[i];
      if (nodeSpecifier instanceof Object[])  // Ie node with children
        child = processHierarchy((Object[])nodeSpecifier);
      else
        child = new DefaultMutableTreeNode(nodeSpecifier); // Ie Leaf
      node.add(child);
    }
    return(node);
  }

    private void close() 
    {
        try {
            if (resultSet != null) 
                        {
                resultSet.close();
            }
            if (statement != null) 
                        {
                //statement.
            }
            if (connect != null) 
                        {
                connect.close();
            }
        } 
        catch (Exception e3) 
                {
        e3.printStackTrace();
        }
    }
}

of course dbaction.java is where I connect to the database and get details.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T00:25:00+00:00Added an answer on June 8, 2026 at 12:25 am

    It is best to create a data model for the tree. How to Use Trees tutorial has good examples. Also go through Understanding the TreeModel for more details.

    By the way, don’t execute long running tasks such as accessing database on Event Dispatch Thread . Look into SwingWorker for such tasks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database with a table named shoppingcarts . this table has the
I have a database Table Named P_Columns. This Table Contains the columns ID Column_Name
I have a database table named 'student' in which there is one column named
I have two columns(column1, column2) in my oracle database table named as demo .
I have a database named contacts.db with a table named TABLE_NAME that has one
I Have a DataBase in my project With Table named 'ProcessData' and columns named
I have a Database (SQL Server) with table named 'ProcessData' and columns named 'Process_Name'
I have a database named Tamaris, which contains a table User. I created a
I have a database table (named Topics) which includes these fields : topicId name
I have a database named crea with a table named assets and in this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.