How to add row in JTable?
Asked Answered
M

5

115

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

Medullated answered 23/8, 2010 at 15:44 Comment(0)
D
197

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

Doughnut answered 23/8, 2010 at 15:58 Comment(3)
+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.Bibliolatry
On creating the table with your first line of code, I get an error "The constructor DefaultTableModel(Object[]) is undefined"Urbanite
@ThisClark: For that DefaultTableModel constructor, you also need to add rowCount, like: JTable table = new JTable(new DefaultTableModel(new Object[]{"Column1", "Column2"}, 20));Wadding
A
60

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"});
Agraphia answered 23/8, 2010 at 15:47 Comment(0)
O
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"});
Orly answered 18/11, 2014 at 20:27 Comment(0)
H
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);

Hardesty answered 28/3, 2014 at 16:53 Comment(0)
C
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});
}
Clerihew answered 4/8, 2015 at 12:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.