I am trying To display the content of the table selected(Imported from MySql) in a JTable whose Structure is unknown to me . I Have Successfully added columns in Jtable but not Able to add rows which the ResultSet Object Returns. Please Help me Insert The Row DATA . I am Attaching the Code.

 private void btnDisplayTableContentActionPerformed(java.awt.event.ActionEvent evt) {                                                       
    try
        { String selTN=cmbTable.getSelectedItem().toString();

          String r="select*from "+selTN;

       TableRecords=  stmt.executeQuery(r);//TableRecords is a resultset object
      rsmd=TableRecords.getMetaData();//rsmd is a Resultsetmetadata object
      int n =  rsmd.getColumnCount();// column no starts from 1

     DefaultTableModel model=(DefaultTableModel)tblDisplay.getModel();

     int i = 1;
      while(i<=n){
      model.addColumn(rsmd.getColumnName(i));// columns suceesfully added
      i++;
      }

      while(TableRecords.next())
      {  int k =1;

             //Unable to add rows Please help      
       }
      tblDisplay.setModel(model);         
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null, e);
        }
    }                

You need to collect all the data for each row as an array of objects - one element per column, then add that array with model.addRow(myArrayOfValues);
You can use a Vector instead of an array if that works better for you

I have Created A AddRow button Which Adds a row to a table (using addRow() ) but I want The Cursor in the 1st cell of add row .. help me .. i dont know which method Should I use .. I used table.requestFocus(); but it does not work ..Help Plzz...

I also used table.changeSelection() but it also didnt worked

changeSelection is the right method, so there's something wrong in the way you are calling it. Without seeing the code I can't comment further.

I have added this code to add new row button which adds an empty row to the jtable.
I want the cursor to be positioned at the 1 cell of the new row added but changeSelection() only selects that row but does not positions the cursor.. help!!!

 private void btnAddRowActionPerformed(java.awt.event.ActionEvent evt) {                                          
   try
   {  tblDisplay.clearSelection();
      model.addRow(vectorAddRow);//empty global string vector 
     tblDisplay.changeSelection(tblDisplay.getRowCount()-1,0, true,false);//srf selction dalta h 
   // does not position the cursor at the first cell of row added 

   }
   catch(Exception e)
   {
     JOptionPane.showMessageDialog(null, e);
   }
    }

Did you enable cell selection?
table.setCellSelectionEnabled(true);

Yes I have enabled the cell selection to true but it only selects that particular cell ,, it does not positions the cursor so that the user can type directly without clicking the desired cell . I want the cursor to be positioned in the cell so that user can directly type without clicking into the cell .. I hope u understood ..

OK
For that you need to override isCellEditable in your table model to return true for the cells you want to be editable by the user. See https://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data

ps: You can often save a lot of time by reading the relevant Orscle tutorial(s) carefullly before trying to code.

Thanks for the advice ..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.