Do you know how I can add a new row to a jTable?
 
    
    - 1,239
- 1
- 16
- 33
 
    
    - 10,778
- 16
- 52
- 70
5 Answers
The TableModel behind the JTable handles all of the data behind the table.  In order to add and remove rows from a table, you need to use a DefaultTableModel
To create the table with this model:
JTable table = new JTable(new DefaultTableModel(new Object[]{"Column1", "Column2"}));
To add a row:
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.addRow(new Object[]{"Column 1", "Column 2", "Column 3"});
You can also remove rows with this method.
Full details on the DefaultTableModel can be found here
- 
                    2+1 This is a good answer; however, note that the DefaultTableModel is only one of many models. However, it is the easiest to use and I would recommend sticking to it unless you have to use another. – chessofnerd Mar 27 '14 at 17:20
- 
                    2On creating the table with your first line of code, I get an error "The constructor DefaultTableModel(Object[]) is undefined" – ThisClark Feb 08 '15 at 20:26
- 
                    6@ThisClark: For that DefaultTableModel constructor, you also need to add rowCount, like: `JTable table = new JTable(new DefaultTableModel(new Object[]{"Column1", "Column2"}, 20));` – Gnaural Sep 29 '15 at 14:28
Use:
DefaultTableModel model = new DefaultTableModel(); 
JTable table = new JTable(model); 
// Create a couple of columns 
model.addColumn("Col1"); 
model.addColumn("Col2"); 
// Append a row 
model.addRow(new Object[]{"v1", "v2"});
 
    
    - 19,333
- 5
- 62
- 89
 
    
    - 325,700
- 82
- 523
- 502
To add row to JTable, one of the ways is:
1) Create table using DefaultTableModel:
        DefaultTableModel model = new DefaultTableModel();
        model.addColumn("Code");
        model.addColumn("Name");
        model.addColumn("Quantity");
        model.addColumn("Unit Price");
        model.addColumn("Price");
        JTable table = new JTable(model);
2) To add row:
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.addRow(new Object[]{"Column 1", "Column 2", "Column 3","Column 4","Column 5"});
 
    
    - 928
- 1
- 11
- 29
 
    
    - 411
- 3
- 14
Use
    DefaultTableModel model = (DefaultTableModel) MyJTable.getModel();
    Vector row = new Vector();
    row.add("Enter data to column 1");
    row.add("Enter data to column 2");
    row.add("Enter data to column 3");
    model.addRow(row);
get the model with DefaultTableModel modelName = (DefaultTableModel) JTabelName.getModel();
Create a Vector with Vector vectorName = new Vector();
add so many row.add as comumns
add soon just add it with modelName.addRow(Vector name);
 
    
    - 123
- 1
- 4
For the sake of completeness, first make sure you have the correct import so you can use the addRow function:
import javax.swing.table.*;
Assuming your jTable is already created, you can proceed and create your own add row method which will accept the parameters that you need:
public void yourAddRow(String str1, String str2, String str3){
  DefaultTableModel yourModel = (DefaultTableModel) yourJTable.getModel();
  yourModel.addRow(new Object[]{str1, str2, str3});
}
 
    
    - 312
- 3
- 12
 
    