113

Do you know how I can add a new row to a jTable?

UrsinusTheStrong
  • 1,239
  • 1
  • 16
  • 33
oneat
  • 10,778
  • 16
  • 52
  • 70

5 Answers5

195

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

Kevin
  • 53,822
  • 15
  • 101
  • 132
Serplat
  • 4,367
  • 2
  • 18
  • 20
  • 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
  • 2
    On 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
59

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"});
kba
  • 19,333
  • 5
  • 62
  • 89
OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
12

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"});
Saikiran Gosikonda
  • 928
  • 1
  • 11
  • 29
8

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);

user3452695
  • 123
  • 1
  • 4
1

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});
}
Joel Karunungan
  • 312
  • 3
  • 12