Solved

populating jtree from table column in oracle db

Posted on 2004-08-18
27
699 Views
Last Modified: 2010-08-05
hello all
i designed an application using java, i put tree control on left and fields on right, i also established database connection, everything is running fine but
now i want to populate this tree and bring out values from a colum in the table, can somebody assist me.

for better picture, im giving my entire code below

in advance many thanks.

--------------------


import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import java.sql.*;
   

public class gui1 extends javax.swing.JFrame {

 // Variables declaration
   
    private JButton jButton1;
    private JLabel jLabel1;
    private JLabel jLabel10;
    private JLabel jLabel11;
    private JLabel jLabel12;
    private JLabel jLabel13;
    private JLabel jLabel14;
    private JLabel jLabel15;
    private JLabel jLabel2;
    private JLabel jLabel3;
    private JLabel jLabel4;
    private JLabel jLabel5;
    private JLabel jLabel6;
    private JLabel jLabel7;
    private JLabel jLabel8;
    private JLabel jLabel9;
    private JLabel jPanel1;
    private JPanel jPanel2;
    private JPanel jPanel3;
    private JPanel jPanel4;
    private JPanel jPanel5;
    private JPanel jPanel6;
    private JPanel jPanel7;
    private JPanel jpanel1;
    private JScrollPane jScrollPane1;
    private JSplitPane jSplitPane1;
    private JTextArea jTextArea1;
    private JTextField jTextField1;
    private JTextField jTextField10;
    private JTextField jTextField11;
    private JTextField jTextField12;
    private JTextField jTextField13;
    private JTextField jTextField14;
    private JTextField jTextField2;
    private JTextField jTextField3;
    private JTextField jTextField4;
    private JTextField jTextField5;
    private JTextField jTextField6;
    private JTextField jTextField7;
    private JTextField jTextField8;
    private JTextField jTextField9;
    private JTree jTree1;
      private Connection connect;  
   
     
    /** Creates new form GUI */
    public gui1() {
        initComponents();
    }
   
    /*This method is called from within the constructor to initialize the form. */
    private void initComponents() {
        jPanel2 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jTextArea1 = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel4 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextField5 = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jTextField6 = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jTextField7 = new javax.swing.JTextField();
        jPanel6 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        jTextField8 = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jTextField9 = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jTextField10 = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jTextField11 = new javax.swing.JTextField();
        jPanel7 = new javax.swing.JPanel();
        jLabel12 = new javax.swing.JLabel();
        jTextField12 = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        jTextField13 = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        jTextField14 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel3 = new javax.swing.JPanel();
       
        /*tree design and declarations*/
       
             
       
        javax.swing.tree.DefaultMutableTreeNode topnode = new javax.swing.tree.DefaultMutableTreeNode("Project. Nr");
        javax.swing.tree.DefaultMutableTreeNode subnode1 = new javax.swing.tree.DefaultMutableTreeNode("Angebots. Nr");
              topnode.add(subnode1);
        javax.swing.tree.DefaultMutableTreeNode subnode2 = new javax.swing.tree.DefaultMutableTreeNode("Version. Nr");
              subnode1.add(subnode2);
        javax.swing.tree.DefaultMutableTreeNode subnode3 = new javax.swing.tree.DefaultMutableTreeNode("UA. Nr");
              subnode2.add(subnode3);
                              
        jTree1 = new javax.swing.JTree(topnode);
       
       
        setTitle("Anska Maske");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        jPanel2.setLayout(new java.awt.BorderLayout());

        jPanel2.setPreferredSize(new java.awt.Dimension(800, 600));
        jPanel1.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT, 10, 20));

        jPanel1.setMinimumSize(new java.awt.Dimension(96, 96));
        jPanel1.setPreferredSize(new java.awt.Dimension(800, 126));
        jPanel1.setAutoscrolls(true);
        jLabel15.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel15.setText("Info / Action");
        jLabel15.setPreferredSize(new java.awt.Dimension(77, 27));
        jLabel15.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentMoved(java.awt.event.ComponentEvent evt) {
                jLabel15ComponentMoved(evt);
            }
        });

        jPanel1.add(jLabel15);

        jTextArea1.setEditable(false);
        jTextArea1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(255, 255, 255)));
        jTextArea1.setPreferredSize(new java.awt.Dimension(690, 50));
        jPanel1.add(jTextArea1);

        jButton1.setText("Transfer ");
        jButton1.setAlignmentX(0.5F);
        jButton1.setBorder(new javax.swing.border.EtchedBorder());
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setPreferredSize(new java.awt.Dimension(97, 27));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jPanel1.add(jButton1);

        jPanel2.add(jPanel1, java.awt.BorderLayout.SOUTH);

        jSplitPane1.setContinuousLayout(true);
        jPanel4.setMinimumSize(new java.awt.Dimension(550, 440));
        jPanel4.setPreferredSize(new java.awt.Dimension(500, 440));
        jPanel5.setLayout(new java.awt.GridLayout(7, 0, 0, 3));

        jPanel5.setBorder(new javax.swing.border.TitledBorder("Angebot"));
        jPanel5.setMinimumSize(new java.awt.Dimension(367, 220));
        jPanel5.setName("");
        jPanel5.setPreferredSize(new java.awt.Dimension(500, 220));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel1.setLabelFor(jPanel5);
        jLabel1.setText("Project. Nr");
        jLabel1.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        jPanel5.add(jLabel1);

        jPanel5.add(jTextField1);

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel2.setLabelFor(jPanel5);
        jLabel2.setText("AngebotsNr");
        jLabel2.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        jPanel5.add(jLabel2);

        jPanel5.add(jTextField2);

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel3.setLabelFor(jPanel5);
        jLabel3.setText("Produkt Bezeichnung");
        jLabel3.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        jPanel5.add(jLabel3);

        jPanel5.add(jTextField3);

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel4.setLabelFor(jPanel5);
        jLabel4.setText("Kunde");
        jLabel4.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        jPanel5.add(jLabel4);

        jPanel5.add(jTextField4);

        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel5.setLabelFor(jPanel5);
        jLabel5.setText("Faktor");
        jLabel5.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        jPanel5.add(jLabel5);

        jPanel5.add(jTextField5);

        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel6.setLabelFor(jPanel5);
        jLabel6.setText("Version Bezugsnummer");
        jLabel6.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        jPanel5.add(jLabel6);

        jPanel5.add(jTextField6);

        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel7.setLabelFor(jPanel5);
        jLabel7.setText("Basis- / Erweiterungsprojekt(e)");
        jLabel7.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        jPanel5.add(jLabel7);

        jPanel5.add(jTextField7);

        jPanel4.add(jPanel5);

        jPanel6.setLayout(new java.awt.GridLayout(4, 2, 0, 3));

        jPanel6.setBorder(new javax.swing.border.TitledBorder("Version und UA-Angaben"));
        jPanel6.setMinimumSize(new java.awt.Dimension(367, 120));
        jPanel6.setPreferredSize(new java.awt.Dimension(500, 120));
        jLabel8.setText("Version");
        jPanel6.add(jLabel8);

        jPanel6.add(jTextField8);

        jLabel9.setText("Gesch\u00e4ftsjahr");
        jPanel6.add(jLabel9);

        jPanel6.add(jTextField9);

        jLabel10.setText("Versions Beschreibung");
        jPanel6.add(jLabel10);

        jPanel6.add(jTextField10);

        jLabel11.setText("UA Beschreibung");
        jPanel6.add(jLabel11);

        jPanel6.add(jTextField11);

        jPanel4.add(jPanel6);

        jPanel7.setLayout(new java.awt.GridLayout(3, 2, 0, 3));

        jPanel7.setBorder(new javax.swing.border.TitledBorder("Ersteller"));
        jPanel7.setMinimumSize(new java.awt.Dimension(367, 100));
        jPanel7.setPreferredSize(new java.awt.Dimension(500, 100));
        jLabel12.setText("Gesamt");
        jPanel7.add(jLabel12);

        jPanel7.add(jTextField12);

        jLabel13.setText("Elektrik");
        jPanel7.add(jLabel13);

        jPanel7.add(jTextField13);

        jLabel14.setText("Mechanik");
        jPanel7.add(jLabel14);

        jPanel7.add(jTextField14);

        jPanel4.add(jPanel7);

        jSplitPane1.setRightComponent(jPanel4);

        jScrollPane1.setBackground(new java.awt.Color(255, 255, 255));
        jScrollPane1.setMaximumSize(new java.awt.Dimension(300, 220));
        jScrollPane1.setMinimumSize(new java.awt.Dimension(100, 22));
        jScrollPane1.setPreferredSize(new java.awt.Dimension(250, 75));
        jPanel3.setLayout(new java.awt.BorderLayout());

            

        jPanel3.add(jTree1, java.awt.BorderLayout.CENTER);

        jScrollPane1.setViewportView(jPanel3);

        jSplitPane1.setLeftComponent(jScrollPane1);

        jPanel2.add(jSplitPane1, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        pack();
       
        /****  database connectivitiy *****/
       
        try
        {
            DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
                     
           
            Connection conn =
            DriverManager.getConnection ("jdbc:oracle:thin:@fe64700:1521:tanska","anska_test", "praktikant2004");
           
            Statement stmt = conn.createStatement ();
                ResultSet rset = stmt.executeQuery("select * from dept1");
           
           
                   while (rset.next())
                  {
                  System.out.println (rset.getString ("dept_name"));
                topnode.add(new DefaultMutableTreeNode(rset.getString("dept_name")));
                  }
        }
                 
        catch (SQLException e)
        {
            System.out.println(e);
        }
       
    }  
   
    private void jLabel15ComponentMoved(java.awt.event.ComponentEvent evt) {
        // code for displaying the data would be written here*
    }
   
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        // action code for button
    }
   
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }
   
    /* database connectivity -  connecting to oracle database*/
   
   
   
   
    /* param args the command line arguments  */
    public static void main(String args[]) {
        setDefaultLookAndFeelDecorated(true);
             
        new gui1().show();
       
       
       
    }
   
   
   
}
0
Comment
Question by:vihar123
  • 14
  • 13
27 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 11828202
Try to add that after you are done with the while loop:

jTree1.setRootVisible(true);

also try to reload your tree model:

treeModel.reload();


0
 

Author Comment

by:vihar123
ID: 11828264
where exactly shld i add this, after the while statement?


  while (rset.next())
               {
               System.out.println (rset.getString ("dept_name"));
              topnode.add(new DefaultMutableTreeNode(rset.getString("dept_name")));
               }

jTree1.setRootVisible(true);

treeModel.reload();


what is treeModel.reload(); -  i dont have any variable like that, its not accepting also
0
 
LVL 35

Expert Comment

by:girionis
ID: 11828304
Yes I'd put it after the while loop. The treeModel should be the model of your tree.
0
 

Author Comment

by:vihar123
ID: 11828365
if you see my code, i dont have any tree model, i desined it the way given below

is there some other way of bringing data it these nodes?


javax.swing.tree.DefaultMutableTreeNode topnode = new javax.swing.tree.DefaultMutableTreeNode("Project. Nr");
        javax.swing.tree.DefaultMutableTreeNode subnode1 = new javax.swing.tree.DefaultMutableTreeNode("Angebots. Nr");
             topnode.add(subnode1);
        javax.swing.tree.DefaultMutableTreeNode subnode2 = new javax.swing.tree.DefaultMutableTreeNode("Version. Nr");
             subnode1.add(subnode2);
        javax.swing.tree.DefaultMutableTreeNode subnode3 = new javax.swing.tree.DefaultMutableTreeNode("UA. Nr");
             subnode2.add(subnode3);
                             
        jTree1 = new javax.swing.JTree(topnode);
0
 
LVL 35

Accepted Solution

by:
girionis earned 500 total points
ID: 11828519
Can you do:

javax.swing.tree.DefaultMutableTreeNode topnode = new javax.swing.tree.DefaultMutableTreeNode("Project. Nr");
        javax.swing.tree.DefaultMutableTreeNode subnode1 = new javax.swing.tree.DefaultMutableTreeNode("Angebots. Nr");
             topnode.add(subnode1);
        javax.swing.tree.DefaultMutableTreeNode subnode2 = new javax.swing.tree.DefaultMutableTreeNode("Version. Nr");
             subnode1.add(subnode2);
        javax.swing.tree.DefaultMutableTreeNode subnode3 = new javax.swing.tree.DefaultMutableTreeNode("UA. Nr");
             subnode2.add(subnode3);
                             
DefaultTreeModel treeModel = new DefaultTreeModel(topnode);

        jTree1 = new javax.swing.JTree(treeModel);

and then after your while loop do:

 while (rset.next())
               {
               System.out.println (rset.getString ("dept_name"));
              topnode.add(new DefaultMutableTreeNode(rset.getString("dept_name")));
               }

jTree1.setRootVisible(true);
treeModel.reload();

0
 

Author Comment

by:vihar123
ID: 11828681
thanks a lot its working
0
 
LVL 35

Expert Comment

by:girionis
ID: 11828710
:)
0
 

Author Comment

by:vihar123
ID: 11829383
How can i redesign my tree structure, i want to retreive employee name in the top node and each name should be a node in itself, when a name is chosen then it should include the subnode1 employee salary and it should further include subnode 2 employee commission.

          .john
            |  |
            |  |-----.Salary
            |             |
            |             |------commission
            |
         .bob

any ideas, please contribute
0
 
LVL 35

Expert Comment

by:girionis
ID: 11829474
What is the current structure of the jtree right now?
0
 

Author Comment

by:vihar123
ID: 11829604
its a normal one, with empnames-displaying all names, then subnode empsal- displaying all salaries...etc

           . Names
               john
                bob        
           . Salary
               1000
               2000
           . Commisiion
                 200
                  200




javax.swing.tree.DefaultMutableTreeNode topnode = new javax.swing.tree.DefaultMutableTreeNode("Project Nr");
       
        javax.swing.tree.DefaultMutableTreeNode subnode1 = new javax.swing.tree.DefaultMutableTreeNode();
              topnode.add(subnode1);
        javax.swing.tree.DefaultMutableTreeNode subnode2 = new javax.swing.tree.DefaultMutableTreeNode();
              subnode1.add(subnode2);
        javax.swing.tree.DefaultMutableTreeNode subnode3 = new javax.swing.tree.DefaultMutableTreeNode();
              subnode2.add(subnode3);
                              
        DefaultTreeModel treeModel = new DefaultTreeModel(topnode);
       
        jTree1 = new javax.swing.JTree(treeModel);
0
 
LVL 35

Expert Comment

by:girionis
ID: 11829658
First of all you will need a root node anyway, something like "information". Then add all main nodes to this node and all subnodes to these nodes. For example if your root node is called rootNode you could do:

DefaultTreeModel treeModel = new DefaultTreeModel(rootNode);

Now in your rootNode add the names of the employers:

DefaultMutableTreeNode john = DefaultMutableTreeNode("john");
DefaultMutableTreeNode bob = DefaultMutableTreeNode("bob");
rootNode.add(john);
rootNode.add(bob);

then keep adding sub-nodes to "john"

DefaultMutableTreeNode johnSalary= DefaultMutableTreeNode("Salary");
john.add(johnSalary);

So you add the "Salary" node to "john" and you add "john" to the root node (you also add bob to the root), so you have something like:

root
      john
          Salary
      bob

Do likewise for as many nodes as you want.
0
 

Author Comment

by:vihar123
ID: 11829732
i want to retreive the names from the database table, so i want this name to be a root note giving access to other information such as salary and commission.

for eg:- i retreived data using the below statement and the names are taken from the db but how to make them rootnodes?

        .empnames
              john
              bob
             using this statement ----> topnode.add(new DefaultMutableTreeNode(rset.getString("emp_name")));

so, actually i want to do something like this, that each name should be a node initself

           .empnames
              .john
                    |
                     .salary
                           |
                           .commission
              .bob
0
 
LVL 35

Expert Comment

by:girionis
ID: 11829803
I woudl do something like that (you might need to paly around to find the exact order you want):

while (rset.next())
               {
               DefaultMutableTreeNode name = DefaultMutableTreeNode(rset.getString("<employer_name>"));
              topnode.add(name);
              DefaultMutableTreeNode salary = DefaultMutableTreeNode(rset.getString("<salary value>"));
              name.add(salary);
              DefaultMutableTreeNode commission = DefaultMutableTreeNode(rset.getString("<commission value"));
              salary.add(commision);
               }

I take for granetd that your result set contains rows with the values of the name of the emplyee, their salary and their commission. You will need to substitute the <salary value> for exampel with the name of the column that represents the salary of the employee.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:vihar123
ID: 11830010
when im doing this, its coming as null pointer exception

java.lang.NullPointerException
        at java.awt.Container.addImpl(Container.java:625)
        at java.awt.Container.add(Container.java:518)
        at gui1.initComponents(gui1.java:309)
        at gui1.<init>(gui1.java:70)
        at gui1.main(gui1.java:390)
Exception in thread "main"

---------


   while (rset.next())
               {
               //System.out.println (rset.getString ("dept_name"));
              //topnode.add(new DefaultMutableTreeNode(rset.getString("emp_name")));
              //subnode1.add(new DefaultMutableTreeNode(rset.getString("emp_sal")));
              //subnode2.add(new DefaultMutableTreeNode(rset.getString("emp_comm")));
              //subnode3.add(new DefaultMutableTreeNode(rset.getString("emp_no")));
              DefaultMutableTreeNode topnode = new javax.swing.tree.DefaultMutableTreeNode(rset.getString("<emp_name>"));
              topnode.add(topnode);
              DefaultMutableTreeNode salary = new javax.swing.tree.DefaultMutableTreeNode(rset.getString("<emp_sal>"));
              topnode.add(salary);
              DefaultMutableTreeNode commission = new javax.swing.tree.DefaultMutableTreeNode(rset.getString("<emp_comm>"));
              salary.add(commission);
               }
               
            
              javax.swing.tree.DefaultTreeModel treeModel = new javax.swing.tree.DefaultTreeModel(topnode);
                jTree1 = new javax.swing.JTree(treeModel);
       
                        jTree1.setRootVisible(true);
                        treeModel.reload();
0
 
LVL 35

Expert Comment

by:girionis
ID: 11830051
Check to see if you are reading the columns from the db correctly. Make sure that you have a column named: <emp_name> for example.
0
 

Author Comment

by:vihar123
ID: 11830346
column names are correct, when im trying this below way, its giving a new error

java.lang.IllegalArgumentException: new child is an ancestor
        at javax.swing.tree.DefaultMutableTreeNode.insert(DefaultMutableTreeNode.java:165)
        at javax.swing.tree.DefaultMutableTreeNode.add(DefaultMutableTreeNode.java:396)
        at gui2.initComponents(gui2.java:344)
        at gui2.<init>(gui2.java:70)
        at gui2.main(gui2.java:392)

--------------------------------------------------





 while (rset.next())
               {
               //System.out.println (rset.getString ("dept_name"));
              //topnode.add(new DefaultMutableTreeNode(rset.getString("emp_name")));
              //subnode1.add(new DefaultMutableTreeNode(rset.getString("emp_sal")));
              //subnode2.add(new DefaultMutableTreeNode(rset.getString("emp_comm")));
              //subnode3.add(new DefaultMutableTreeNode(rset.getString("emp_no")));
              topnode.add(new javax.swing.tree.DefaultMutableTreeNode(rset.getString("emp_name")));
              topnode.add(topnode);
              subnode1.add(new javax.swing.tree.DefaultMutableTreeNode(rset.getString("emp_sal")));
              topnode.add(subnode1);
              subnode2.add(new javax.swing.tree.DefaultMutableTreeNode(rset.getString("emp_comm")));
              subnode2.add(subnode1);
               }
               
         
              //javax.swing.tree.DefaultTreeModel treeModel = new javax.swing.tree.DefaultTreeModel(topnode);
               //jTree1 = new javax.swing.JTree(treeModel);
     
       
       jTree1 = new javax.swing.JTree(treeModel);
                    jTree1.setRootVisible(true);
                    treeModel.reload();

                  
0
 
LVL 35

Expert Comment

by:girionis
ID: 11830395
> topnode.add(topnode);

You cannot add a node to itself.
0
 

Author Comment

by:vihar123
ID: 11830561
when im doing this way then its giving a runtime error as

java.lang.NullPointerException
        at gui2.initComponents(gui2.java:351)
        at gui2.<init>(gui2.java:70)
        at gui2.main(gui2.java:400)

---------------------------------------------


while (rset.next())
               {
                              
                DefaultMutableTreeNode name1 = new DefaultMutableTreeNode(rset.getString("emp_name"));
              topnode.add(name1);
              DefaultMutableTreeNode salary = new DefaultMutableTreeNode(rset.getString("emp_sal"));
              name1.add(salary);
              DefaultMutableTreeNode commission = new DefaultMutableTreeNode(rset.getString("emp_comm"));
              salary.add(commission);

               }
               
         
                    jTree1 = new javax.swing.JTree(treeModel);
                    jTree1.setRootVisible(true);
                    treeModel.reload();
0
 
LVL 35

Expert Comment

by:girionis
ID: 11832942
Which line is the npe?
0
 

Author Comment

by:vihar123
ID: 11838715
sorry i dont understand what npe is, this is my entire code...........


---------


import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import java.sql.*;
   

public class gui2 extends javax.swing.JFrame {

 // Variables declaration
   
    private JButton jButton1;
    private JLabel jLabel1;
    private JLabel jLabel10;
    private JLabel jLabel11;
    private JLabel jLabel12;
    private JLabel jLabel13;
    private JLabel jLabel14;
    private JLabel jLabel15;
    private JLabel jLabel2;
    private JLabel jLabel3;
    private JLabel jLabel4;
    private JLabel jLabel5;
    private JLabel jLabel6;
    private JLabel jLabel7;
    private JLabel jLabel8;
    private JLabel jLabel9;
    private JLabel jPanel1;
    private JPanel jPanel2;
    private JPanel jPanel3;
    private JPanel jPanel4;
    private JPanel jPanel5;
    private JPanel jPanel6;
    private JPanel jPanel7;
    private JPanel jpanel1;
    private JScrollPane jScrollPane1;
    private JSplitPane jSplitPane1;
    private JTextArea jTextArea1;
    private JTextField jTextField1;
    private JTextField jTextField10;
    private JTextField jTextField11;
    private JTextField jTextField12;
    private JTextField jTextField13;
    private JTextField jTextField14;
    private JTextField jTextField2;
    private JTextField jTextField3;
    private JTextField jTextField4;
    private JTextField jTextField5;
    private JTextField jTextField6;
    private JTextField jTextField7;
    private JTextField jTextField8;
    private JTextField jTextField9;
    private JTree jTree1;
      private Connection connect;
      private DefaultMutableTreeNode topnode;
      
      
    /** Creates new form GUI */
    public gui2() {
        initComponents();
    }
   
    /*This method is called from within the constructor to initialize the form. */
    private void initComponents() {
        jPanel2 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jTextArea1 = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel4 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextField5 = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jTextField6 = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jTextField7 = new javax.swing.JTextField();
        jPanel6 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        jTextField8 = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jTextField9 = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jTextField10 = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jTextField11 = new javax.swing.JTextField();
        jPanel7 = new javax.swing.JPanel();
        jLabel12 = new javax.swing.JLabel();
        jTextField12 = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        jTextField13 = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        jTextField14 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel3 = new javax.swing.JPanel();
       
        /*tree design and declarations*/
   
         
       
        /*javax.swing.tree.DefaultMutableTreeNode topnode = new javax.swing.tree.DefaultMutableTreeNode("Project Nr");
       
        javax.swing.tree.DefaultMutableTreeNode subnode1 = new javax.swing.tree.DefaultMutableTreeNode("Version Nr");
              topnode.add(subnode1);
        javax.swing.tree.DefaultMutableTreeNode subnode2 = new javax.swing.tree.DefaultMutableTreeNode("Versions");
              subnode1.add(subnode2);
        javax.swing.tree.DefaultMutableTreeNode subnode3 = new javax.swing.tree.DefaultMutableTreeNode("UA");
              subnode2.add(subnode3);*/
                              
        //DefaultTreeModel treeModel = new DefaultTreeModel(topnode);
       
       //jTree1 = new javax.swing.JTree(treeModel);

      //jTree1 = new javax.swing.JTree(topnode);
       
       
        setTitle("Anska Maske");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        jPanel2.setLayout(new java.awt.BorderLayout());

        jPanel2.setPreferredSize(new java.awt.Dimension(800, 600));
        jPanel1.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT, 10, 20));

        jPanel1.setMinimumSize(new java.awt.Dimension(96, 96));
        jPanel1.setPreferredSize(new java.awt.Dimension(800, 126));
        jPanel1.setAutoscrolls(true);
        jLabel15.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel15.setText("Info / Action");
        jLabel15.setPreferredSize(new java.awt.Dimension(77, 27));
        jLabel15.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentMoved(java.awt.event.ComponentEvent evt) {
                jLabel15ComponentMoved(evt);
            }
        });

        jPanel1.add(jLabel15);

        jTextArea1.setEditable(false);
        jTextArea1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(255, 255, 255)));
        jTextArea1.setPreferredSize(new java.awt.Dimension(690, 50));
        jPanel1.add(jTextArea1);

        jButton1.setText("Transfer ");
        jButton1.setAlignmentX(0.5F);
        jButton1.setBorder(new javax.swing.border.EtchedBorder());
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setPreferredSize(new java.awt.Dimension(97, 27));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jPanel1.add(jButton1);

        jPanel2.add(jPanel1, java.awt.BorderLayout.SOUTH);

        jSplitPane1.setContinuousLayout(true);
        jPanel4.setMinimumSize(new java.awt.Dimension(550, 440));
        jPanel4.setPreferredSize(new java.awt.Dimension(500, 440));
        jPanel5.setLayout(new java.awt.GridLayout(7, 0, 0, 3));

        jPanel5.setBorder(new javax.swing.border.TitledBorder("Angebot"));
        jPanel5.setMinimumSize(new java.awt.Dimension(367, 220));
        jPanel5.setName("");
        jPanel5.setPreferredSize(new java.awt.Dimension(500, 220));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel1.setLabelFor(jPanel5);
        jLabel1.setText("Project. Nr");
        jLabel1.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        jPanel5.add(jLabel1);

        jPanel5.add(jTextField1);

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel2.setLabelFor(jPanel5);
        jLabel2.setText("AngebotsNr");
        jLabel2.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        jPanel5.add(jLabel2);

        jPanel5.add(jTextField2);

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel3.setLabelFor(jPanel5);
        jLabel3.setText("Produkt Bezeichnung");
        jLabel3.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        jPanel5.add(jLabel3);

        jPanel5.add(jTextField3);

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel4.setLabelFor(jPanel5);
        jLabel4.setText("Kunde");
        jLabel4.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        jPanel5.add(jLabel4);

        jPanel5.add(jTextField4);

        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel5.setLabelFor(jPanel5);
        jLabel5.setText("Faktor");
        jLabel5.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        jPanel5.add(jLabel5);

        jPanel5.add(jTextField5);

        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel6.setLabelFor(jPanel5);
        jLabel6.setText("Version Bezugsnummer");
        jLabel6.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        jPanel5.add(jLabel6);

        jPanel5.add(jTextField6);

        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel7.setLabelFor(jPanel5);
        jLabel7.setText("Basis- / Erweiterungsprojekt(e)");
        jLabel7.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        jPanel5.add(jLabel7);

        jPanel5.add(jTextField7);

        jPanel4.add(jPanel5);

        jPanel6.setLayout(new java.awt.GridLayout(4, 2, 0, 3));

        jPanel6.setBorder(new javax.swing.border.TitledBorder("Version und UA-Angaben"));
        jPanel6.setMinimumSize(new java.awt.Dimension(367, 120));
        jPanel6.setPreferredSize(new java.awt.Dimension(500, 120));
        jLabel8.setText("Version");
        jPanel6.add(jLabel8);

        jPanel6.add(jTextField8);

        jLabel9.setText("Gesch\u00e4ftsjahr");
        jPanel6.add(jLabel9);

        jPanel6.add(jTextField9);

        jLabel10.setText("Versions Beschreibung");
        jPanel6.add(jLabel10);

        jPanel6.add(jTextField10);

        jLabel11.setText("UA Beschreibung");
        jPanel6.add(jLabel11);

        jPanel6.add(jTextField11);

        jPanel4.add(jPanel6);

        jPanel7.setLayout(new java.awt.GridLayout(3, 2, 0, 3));

        jPanel7.setBorder(new javax.swing.border.TitledBorder("Ersteller"));
        jPanel7.setMinimumSize(new java.awt.Dimension(367, 100));
        jPanel7.setPreferredSize(new java.awt.Dimension(500, 100));
        jLabel12.setText("Gesamt");
        jPanel7.add(jLabel12);

        jPanel7.add(jTextField12);

        jLabel13.setText("Elektrik");
        jPanel7.add(jLabel13);

        jPanel7.add(jTextField13);

        jLabel14.setText("Mechanik");
        jPanel7.add(jLabel14);

        jPanel7.add(jTextField14);

        jPanel4.add(jPanel7);

        jSplitPane1.setRightComponent(jPanel4);

        jScrollPane1.setBackground(new java.awt.Color(255, 255, 255));
        jScrollPane1.setMaximumSize(new java.awt.Dimension(300, 220));
        jScrollPane1.setMinimumSize(new java.awt.Dimension(100, 22));
        jScrollPane1.setPreferredSize(new java.awt.Dimension(250, 75));
        jPanel3.setLayout(new java.awt.BorderLayout());

            

        jPanel3.add(jTree1, java.awt.BorderLayout.CENTER);

        jScrollPane1.setViewportView(jPanel3);

        jSplitPane1.setLeftComponent(jScrollPane1);

        jPanel2.add(jSplitPane1, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        pack();
       
        /****  database connectivitiy *****/
       
        try
        {
            DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
                     
           
            Connection conn =
            DriverManager.getConnection ("jdbc:oracle:thin:@fe64700:1521:tanska","anska_test", "praktikant2004");
           
            Statement stmt = conn.createStatement ();
                ResultSet rset = stmt.executeQuery("select * from emp1");
           

           
                    while (rset.next())
               {
               //System.out.println (rset.getString ("dept_name"));
              //topnode.add(new DefaultMutableTreeNode(rset.getString("emp_name")));
              //subnode1.add(new DefaultMutableTreeNode(rset.getString("emp_sal")));
              //subnode2.add(new DefaultMutableTreeNode(rset.getString("emp_comm")));
              //subnode3.add(new DefaultMutableTreeNode(rset.getString("emp_no")));
              //topnode.add(new DefaultMutableTreeNode(rset.getString("emp_name")));
              //topnode.add(topnode);
              //subnode1.add(new DefaultMutableTreeNode(rset.getString("emp_sal")));
              //topnode.add(subnode1);
              //subnode2.add(new DefaultMutableTreeNode(rset.getString("emp_comm")));
              //subnode2.add(subnode1);
                
                DefaultMutableTreeNode name1 = new DefaultMutableTreeNode(rset.getString("emp_name"));
              topnode.add(name1);
              DefaultMutableTreeNode salary = new DefaultMutableTreeNode(rset.getString("emp_sal"));
              name1.add(salary);
              DefaultMutableTreeNode commission = new DefaultMutableTreeNode(rset.getString("emp_comm"));
              salary.add(commission);

               }
               
         
              //javax.swing.tree.DefaultTreeModel treeModel = new javax.swing.tree.DefaultTreeModel(topnode);
               //jTree1 = new javax.swing.JTree(treeModel);
      DefaultTreeModel treeModel = new DefaultTreeModel(topnode);
       
       jTree1 = new javax.swing.JTree(treeModel);

      jTree1 = new javax.swing.JTree(topnode);
       
                         jTree1 = new javax.swing.JTree(treeModel);
                    jTree1.setRootVisible(true);
                    treeModel.reload();

                  
        }
                 
        catch (SQLException e)
        {
            System.out.println(e);
        }
       
    }  
   
    private void jLabel15ComponentMoved(java.awt.event.ComponentEvent evt) {
        // code for displaying the data would be written here*
    }
   
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        // action code for button
    }
   
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }
   
    /* database connectivity -  connecting to oracle database*/
   
   
   
   
    /* param args the command line arguments  */
    public static void main(String args[]) {
        setDefaultLookAndFeelDecorated(true);
             
        new gui2().show();
       
       
       
    }
   
   
   
}
0
 
LVL 35

Expert Comment

by:girionis
ID: 11838798
Npe is a short for NullPointerException instead of writting the whole word most of the java programmers just use the "npe" :)

Can you tell me in which line the npe occurs?
0
 

Author Comment

by:vihar123
ID: 11838865
at gui2.initComponents(gui2.java:351) ------>   topnode.add(name1);


at gui2.<init>(gui2.java:70) ----->    initComponents();


at gui2.main(gui2.java:404) ------ >  new gui2().show();
0
 
LVL 35

Expert Comment

by:girionis
ID: 11838941
I do not see you initialize the topnode anywhere? You declare it but not initialize it:

private DefaultMutableTreeNode topnode;

You need to do something like:

private DefaultMutableTreeNode topnode = new DefaultMutableTreeNode("Project Nr");

The other place where you declared and initialized it is within comments.
0
 

Author Comment

by:vihar123
ID: 11838961
thanks , i cleared it......it was a simple mistake.....i commented topnode :-)
0
 
LVL 35

Expert Comment

by:girionis
ID: 11839049
:)
0
 

Author Comment

by:vihar123
ID: 11840078
how to display other values in the adjacent textfields. i mean when user selects a name, i want to display some information from the other table or same table on the adjcent available textfields.

please contibute....

0
 
LVL 35

Expert Comment

by:girionis
ID: 11840122
I think you better ask a new quetion for this as other experts might be able to help you too :)
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

743 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

16 Experts available now in Live!

Get 1:1 Help Now