Inserting Rows on a JTabel

Leave a Comment

DefaultTableModel(Object data[][], Object col[]):
This method creates a DefaultTableModel and initializes the table that will pass in it. It takes the following arguments:

  • data: This is the object that adds in a table.
  • col: This is a column object that adds in the table.

insertRow(int row_index, Object data[]):
The above method is used to insert a row at specified location. It takes the following parameters:

    row_index: This is the index of row that to be added.
  data: This is the data that have to add in the table.
Example snippet of a code

DefaultTableModel model = new DefaultTableModel(data,col);
//creates the default model of the table
  JTable table = new JTable(model);
  //Insert a row in first position
  model.insertRow(0,new Object[]{"Jahson","50"});
  //Insert a row in  4th  position
  model.insertRow(3,new Object[]{"Moses","600"});
  //Insert last position
  model.insertRow(table.getRowCount(),new Object[]{"John","600"});


The above code snippets  if used in a code to insert rows will add the rows in the rows in the first , 4th position and last position respectively.
Remember that counting in programming begins from zero thats why we are saying that this code    model.insertRow(0,new Object[]{"Jahson","50"})
will insert the row in position one.



0 comments:

Post a Comment