Monday, 25 March 2013

How to create a dynamic(growable with new entries) grid table in Java using Netbeans

Here i'm going to explain is how to create a Dynamic Grid table in core java using Netbeans.

Step 1: Create a Java application from File->New Project

Step 2: In Design part add the table from the Palette Swing Controls.If palette tab is not visible you can choose it from your  Tools->Palette->Swing/AWT Components.

Step 3: You can Change your table column name by right clicking the table and choose Table Contents and then choose columns tabs.

Step 4: Then in your java code you can see your table name as "jTable1" like that it will be declared as private and it can be declare as public by right clicking and choose customize code.




Object ob[][];  /*for storing values as object in table*/

 int row,i,j; /* creating rows and columns for table */



Here is the code i used for my table.

 try
        {
            st=con.createStatement();
            rs2=st.executeQuery("select * from status where userid='"+empid+"'");
            while(rs2.next())
            {
                row=rs2.getRow();
            }
            ob=new Object[row][6];
            for(i=0;i<row;i++)
            {
                for(j=0;j<6;j++)
                    ob[i][j]=null;
            }
            rs1=st.executeQuery("select fullname from personal where userid='"+empid+"'");
            while(rs1.next())
            {
                name=rs1.getString("fullname");
            }
            empname.setText(name);
        }
        catch(SQLException e)
        {
            System.out.println(e);
        }
        int r=0,intime,outtime;
        try
        {
            st=con.createStatement();
            rs=st.executeQuery("select * from status where userid='"+empid+"'");
            statustable.setModel(new javax.swing.table.DefaultTableModel(ob,new String [] {"User id ", "In time", "Out time", "Work hours", "Salary", "Date"}));
            while(rs.next())
            {
                
                statustable.setValueAt(rs.getString("userid"),r,0);
                intime=rs.getInt("intime");
                outtime=rs.getInt("outtime");
                if(intime>12)   
                {
                    intime=intime-12;
                    statustable.setValueAt(intime+" PM",r,1);
                }
                else
                    statustable.setValueAt(intime+" AM",r,1);
                if(outtime>12)   
                {
                    outtime=outtime-12;
                    statustable.setValueAt(outtime+" PM",r,2);
                }
                else
                    statustable.setValueAt(outtime+" AM",r,2);
                statustable.setValueAt(rs.getInt("workhours"),r,3);
                statustable.setValueAt(rs.getInt("salary"),r,4);
                statustable.setValueAt(rs.getString("date"),r,5);
                r++; 
            }
        }
        catch(SQLException e)
        {
            System.out.println(e);
        }

If you have any queries please post your comments.

No comments:

Post a Comment