Example : How to move/transfer data from java controls to java table (jtable).

public class registration extends javax.swing.JFrame 
{
    int RowId =0;
    
    //static Connection conn = null;
    //Statement st = null;
    //PreparedStatement pst = null;
    //ResultSet rs; 
    
    public registration() 
    {
      initComponents();


       private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) 
        {                                         
           String gender = "";
           if(jRdbRegMale.isSelected())
           {
             gender="Male";
           }
           if(jRdbRegFemale.isSelected())
           {
             gender="Female";
           }
           if(jRdbRegOther.isSelected())
           {
             gender="Other";
           }
        
           String CmbState1 = (String)jCmbRegState.getSelectedItem();
        
           String ChkMatric = "";
           String ChkInter = "";
           String ChkGrad = "";
           String ChkPg = "";
           if(jChkRegMatric.isSelected())
             {
               ChkMatric="Matric";
             }
           else
             { 
               ChkMatric ="null";
             }

           if(jChkRegInter.isSelected())
             {
               ChkInter="Inter";
             }
           else
             { 
               ChkInter ="null";
             }

           if(jChkRegGrad.isSelected())
            {
              ChkGrad="Graduation";
            }
           else
            { 
              ChkGrad ="null";
            }

           if(jChkRegPg.isSelected())
            {
              ChkPg="PG";
            }
           else
            { 
              ChkPg ="null";
            }
        
           Date dd=jDtRegDob.getDate();        //import java.util.Date;
           Format xx= new SimpleDateFormat("dd-MMM-yyyy");    //import java.text.Format; & import 
                                                              java.text.SimpleDateFormat;
           String pp =xx.format(dd);
                 
           String TxtSlno = jTxtRegSlno.getText();
           String TxtName = jTxtRegName.getText();
           String TxtFname = jTxtRegFname.getText();
           String TxtMobile = jTxtRegMobile.getText();
           String TxtEmail = jTxtRegEmail.getText();
           String RtbAddress = jRtbRegAddress.getText();
           String TxtUname = jTxtRegUname.getText();
           String PdPsd = new String(jPdRegPsd.getPassword());
        
           RowId = RowId+1;
        
           DefaultTableModel obj=(DefaultTableModel)jTable1.getModel();
           obj.addRow(new Object[] 
               {RowId,TxtSlno,TxtName,TxtFname,pp,gender,ChkMatric,ChkInter,ChkGrad,ChkPg,TxtMobile,
               TxtEmail,CmbState1,RtbAddress,TxtUname,PdPsd});    
    }

Example : How to move/transfer data from java table (jtable) to java controls on clicking table row.

private void jTable1MouseClicked(java.awt.event.MouseEvent evt) 
{                                     
   
    try
      {
        DefaultTableModel obj= (DefaultTableModel)jTable1.getModel();
        
        jTxtRegSlno.setText(obj.getValueAt(jTable1.getSelectedRow(),1).toString());
        jTxtRegName.setText(obj.getValueAt(jTable1.getSelectedRow(),2).toString());
        jTxtRegFname.setText(obj.getValueAt(jTable1.getSelectedRow(),3).toString());

        String dd =  obj.getValueAt(jTable1.getSelectedRow(),4).toString();        
        jDtRegDob.setDate(new SimpleDateFormat("dd-MMM-yyyy").parse(dd));
        
        String gender = obj.getValueAt(jTable1.getSelectedRow(),5).toString();
            if(gender.equals("Male"))
            {
                jRdbRegMale.setSelected(true);
            }
            else if (gender.equals("Female"))
            {
                jRdbRegFemale.setSelected(true);
            }
            else if (gender.equals("Other"))
            {
                jRdbRegMale.setSelected(true);
            }
            else 
            {
                jRdbRegMale.setSelected(false);
                jRdbRegFemale.setSelected(false);
                jRdbRegOther.setSelected(false);
            }
            
            String ChkMatric = obj.getValueAt(jTable1.getSelectedRow(),6).toString();
                    if (ChkMatric.equals("Matric"))
                    {
                        jChkRegMatric.setSelected(true);
                    }
                    else 
                    {
                        jChkRegMatric.setSelected(false);
                    }

            String ChkInter = obj.getValueAt(jTable1.getSelectedRow(),7).toString();
                    if (ChkInter.equals("Inter"))
                    {
                        jChkRegInter.setSelected(true);
                    }
                    else 
                    {
                        jChkRegInter.setSelected(false);
                    }
            String ChkGrad = obj.getValueAt(jTable1.getSelectedRow(),8).toString();
                    if (ChkGrad.equals("Graduation"))
                    {
                        jChkRegGrad.setSelected(true);
                    }
                    else 
                    {
                        jChkRegGrad.setSelected(false);
                    }
            String ChkPg = obj.getValueAt(jTable1.getSelectedRow(),9).toString();
                    if (ChkPg.equals("PG"))
                    {
                        jChkRegPg.setSelected(true);
                    }
                    else 
                    {
                        jChkRegPg.setSelected(false);
                    }
            
            jTxtRegMobile.setText(obj.getValueAt(jTable1.getSelectedRow(),10).toString());
            jTxtRegEmail.setText(obj.getValueAt(jTable1.getSelectedRow(),11).toString());
            jCmbRegState.setSelectedItem(obj.getValueAt(jTable1.getSelectedRow(),12).toString());
            jRtbRegAddress.setText(obj.getValueAt(jTable1.getSelectedRow(),13).toString());

            jTxtRegUname.setText(obj.getValueAt(jTable1.getSelectedRow(),14).toString());
            jPdRegPsd.setText(obj.getValueAt(jTable1.getSelectedRow(),15).toString());
        }
        catch(Exception e)
        {
           System.out.println(e); 
        }
}

Example : How to hide/unhide (display) the table contents.

private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) 
{                                               
        // TODO add your handling code here:
        if (jToggleButton1.getText().equals("Hide"))
          {
            jTable1.setVisible(false);     // To hide all table data.
            jToggleButton1.setText("Show");            
          }
        else 
          {          
            jTable1.setVisible(true);     // To unhide all table data.
            jToggleButton1.setText("Hide");
          }
}

 5,747 total views,  6 views today

Categories: Java Project

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.